Can't create IBOutlet for UIBarButtonItem in a UIViewController - ios

I'm trying to create an IBoutlet from a UIBarButtonItem to a UIViewController, but when ctrl+clicking and dragging to the controller, I see no indicator whatsoever, and thus it's impossible to create it. Not sure what I'm doing wrong.
Here's the screenshot, in case it could clear something up:
Thanks a lot in advance!
EDIT: I also tried to create the IBOutlet manually with #property (weak,nonatomic) IBOutlet UIBarButtonItem *barButtonItem; and then ctrl+clicking and dragging, but same result.

I was able to create a UIBarButtonItem IBOutlet
#property (nonatomic, weak) IBOutlet UIBarButtonItem *cameraLibraryToggleButton;

if u used story board then select UIBarButtonItem item and just put on the view controller.then, set IBOutlet. try this

I do not think you can create an IBOutlet for a UIBarButtonItem... Instead create a IBOutlet for your UIToolbar and give your UIBarButtonItem a tag of 1. Next use this code:
for(UIBarButtonItem *button in self.toolBar.items)
{
if(button.tag == 1)
{
UIBarButtonItem *newButton = button; //Your UIBarButton
//Do whatever you want with it.
}
}

Make Sure you have assign class (e.g .UIViewController) to your storyBoard

Related

Button moves when zooming image. Objective-c

Could anyone please help me. When I zoom an image button changes its position. Do not know how to heal it.. I assume that might be because they are under "ScrollView" but when I add buttons to "View" they don't respond, they only respond in "ScrollView".
*.h
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIScrollViewDelegate> {
UIImageView *imageView;
UIButton *addImageBtn;
}
#property (nonatomic, retain) IBOutlet UIButton *addImageBtn;
*.m
#synthesize addImageBtn;
Thanks in advance!
Do you really need the buttons inside the scroll? Is autolayout on ? Attach the buttons to the bottom of the container view and put it outside the scroll

How to subclass a UITabBarController and replace its UITabBar view?

I need to subclass a UITabBarController so that I can completely replace the UITabBar view with a custom view that I can hopefully produce in the interface builder. I tried but am not succeeding.
First, I created a subclass of UITabBarController along with a xib. I deleted the default view in the xib, and replaced it with a new one that was only 60px tall (the size of my tabbar). I dragged the necessary buttons onto it, and configured the .h file like so:
#interface ToolbarViewController : UITabBarController
#property (strong, nonatomic) IBOutlet UIView *tabBarView;
#property (strong, nonatomic) IBOutlet UIButton* firstButton;
#property (strong, nonatomic) IBOutlet UIButton* secondButton;
#end
My xib looks like this:
When I launch the app, I see an empty space at the bottom made for the tab bar, but I am not seeing an actual tab bar:
Update: I realize that I'm not actually launching the xib file in the .m file. Anyone know how I can do this properly?
There are various different solutions for adding a custom set of buttons to a custom tab bar controller subclass. I've done it years ago following this guide: http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/.
The idea is to add a custom UIView over the tab bar of your UITabBarController subclass. The CustomTabBarController class doesn't have to have a xib. Instead, I have a subclass of UIView that can either be programmatically laid out, or created using a xib for a UIView. Here's the header file for my CustomTabBarView class:
#interface CustomTabBarView : UIView
{
CALayer *opaqueBackground;
UIImageView *tabBG;
IBOutlet UIButton *button0;
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
NSArray *tabButtons;
int lastTab;
}
#property (nonatomic, weak) id delegate;
-(IBAction)didClickButton:(id)sender;
You'll either connect the desired buttons to button0, button1, button2, etc in the xib file, or do it programmatically on init for the view. Note that this is the UIView subclass.
In CustomTabBarView.m:
-(IBAction)didClickButton:(id)sender {
int pos = ((UIButton *)sender).tag;
// or some other way to figure out which tab button was pressed
[self.delegate setSelectedIndex:pos]; // switch to the correct view
}
Then in your CustomTabBarController class:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
tabView = [[CustomTabBarView alloc] init];
tabView.delegate = self;
tabView.frame = CGRectMake(0, self.view.frame.size.height-60, 320, 60);
[self.view addSubview:tabView];
}
When the buttons are clicked in the CustomTabBarView, it will call its delegate function, in this case the CustomTabBarController. The call is the same function as if you clicked on a tab button in the actual tab bar, so it will jump to the tabs if you have set up the CustomTabBarController correctly like a normal UITabBarController.
Oh, on a slightly separate note, the correct way to add a custom xib as the interface for a subclass of UIView:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
//Just in case the size is different (you may or may not want this)
mainView.frame = self.bounds;
[self addSubview:mainView];
}
return self;
}
In the xib file, make sure the File's Owner has its Custom class set as CustomTabBarView.

Hide UIImageView from other UIViewController

I have maybe stupid question but it is possible to set UIImageView hidden from other class?
I create property of it in one class and want to set it hide in other class. I try it but it is not working.
Here is what i am trying to do:
FirstClass.h:
#property (weak, nonatomic) IBOutlet UIImageView *image;
OtherClass.h:
#property FirstClass*firstClass;
OtherClass.m:
#synthesize fistClass;
and in my method:
[firstClass.image setHidden:YES];
Thanks for any help :)
Have you tried
[self.firstClass.image setHidden:YES];
If firstClass is a property in OtherClass, in an OtherClass method you need to reference the property on self. Also, as mentioned by YarGnawh, make sure your outlet is linked in the storyboard and that you are calling your code in or after awakeFromNib.
It's should work. Make sure you image view is linked up in your storyboard/xib

How do I place a slider value in label?

I am quite new to Xcode and I am having trouble placing the value of a slider in a label when the slider is value is changed.
Here is my code:
Header file:
#interface ViewTwoViewController : UIViewController
{
IBOutlet UISlider *slider;
IBOutlet UILabel *sliderLabel;
}
-(IBAction)sliderValue:(id)sender;
and here is my m file:
-(IBAction)sliderValue:(UISlider *)sender {
sliderLabel.text = [NSString stringWithFormat:#"%g", slider.value];
}
I am not 100% sure why the value isn't updating?
I am looking forward to hearing from you all!
Here's an XCode project that does what you're looking for: http://clrk.it/013r203C1y1F
Note that:
The label and slider are hooked up to the UIViewController as
IBOutlets in the storyboard.
The slider's valueChanged: method is linked to the UIViewController in the storyboard.
The slider's value is displayed using %f.
Your method would look something like this:
-(IBAction)sliderValueChanged:(id)sender
{
if (sender == _slider) {
_label.text = [NSString stringWithFormat:#"%0.3f", _slider.value];
}
}
Here would be my process to figure out the root cause of this problem.
Did you remember to connect the slider and sliderLabel outlets?
Did you remember to connect the IBAction to the slider? Set a breakpoint. When you drag the slider, does sliderValue: get called? What are the values of slider and sliderLabel?
Its unusual that your IBOutlets are not properties. I would expect to see:
#property (weak, nonatomic) IBOutlet UISlider *slider; and
#property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
I tried your code and seems to be correct..Please check these things:-
1) Have you connected Slider action Value Changed to IBAction method for proper functioning.
2) Check our connections with Outlets.
Hope helps you out..

can not move buttons

I have a little problem that my buttons doesn't move when I am using a code that should move the button.
ViewControlle.h:
#property (strong, nonatomic) IBOutlet UIButton *button1;
#property (strong, nonatomic) IBOutlet UIButton *button2;
ViewController.m:
#implementation GameViewController
#synthesize button1,button2;
code which should move the buttons:
button1.center=CGPointMake(button1.center.x, button1.center.y-10);
button2.center=CGPointMake(button2.center.x, button2.center.y+10);
why does the code not work or am I doing something wrong?
Because autolayout did not happen before view did load it will reset your locations.
when - (void)viewDidLayoutSubviews gets called its done so place your code here.
If you are using AutoLayout for ViewController than It's not work.
Uncheck the AutoLayout for ViewController.

Resources