Change a UIBarButtonItem's style to a BarButtonSystemItem - iphone

I'm new to iPhone development, so I'm not sure if it is a very common issue. I didn't find anything about this on Google.
I have a UIBarButtonItem defined with Interface Builder:
#property (weak, nonatomic) IBOutlet UIBarButtonItem *cameraButton;
For now, it's just a simple button, but I want to put a camera image on it, using UIBarButtonSystemItemCamera. I know it's possible to do it with initWithBarButtonSystemItem, but I there is no method like changeWithBarButtonSystemItem. I'd like to something like:
- (void)viewDidLoad
{
[super viewDidLoad];
[cameraButton changeWithBarButtonSystemItem:UIBarButtonSystemItemCamera];
}
How is it possible ? Is there a way to tell Interface Builder to instantiate directly the button with this camera icon ?
Edit
It seems that it's not possible to change a button style after its instantiation. So only one question remains: Is there a way to tell Interface Builder to instantiate a button with UIBarButtonSystemItemCamera ?

In Interface Builder, change the UIBarButtonItem's "Identifier" to "Camera."
If you need to change buttons at run-time, the easiest thing is to swap out buttons themselves, not try to mutate them.

Related

IBAction and IBOutlet Clarification

I am new to Xcode and I am wondering, what does IBAction and IBOutlet do? I've tried doing simple task like 'hello world' but it seems I stuff it up. I am making app that involves a questionnaire that links to a database.
IBAction is used for Methods which performs as a result of any Action for example Button Press.
-(IBAction) buttonPress : (id) sender;
IBOutlet is used for UI related objects like Button,label, View etc.
IBOutlet UILabel *nameLabel;
Note: If you are using XIB for development, you should make use of IBAction and IBOutlet. Otherwise you will not able to map objects and methods on XIB. If you are developing everything by coding then IBAction and IBOutlet are optional.
As mentioned in the answer linked above: "IBAction and IBOutlet are macros defined to denote variables and methods that can be referred to in Interface Builder."
However in layman's terms and a simple way to think of them -
IBActions mark the methods that will be called when an event (e.g. touch down) is triggered on one of your interface builder controls (e.g. button, switch etc).
IBOutlets mark the variable references for your interface builder controls.
Outlets allow you to programatically interact with controls you layout on interface builder.

Changing the Image of a UIButton at Runtime

I have UIButton instance called card. I have an object called "Representer", which has a property "image", which is a UIImage. I'm trying to change the value of the button's image at runtime. I have the button in interface builder, and also declare an IBOutlet with the button. I write this code :
UIButton *card1 = [[UIButton alloc] init];
[card1 setImage:representer.image forState:UIControlStateNormal];
When this code is compiled, the button's image doesn't change. Someone help me, please!
You first need to ensure that your button is connected to the IBOutlet in Interface Builder, have you done that with the Connections Inspector?
If you have the button in your .h as:
#property (nonatomic, retain) IBOutlet UIButton *option1Button;
Then, in the .m file, call
[option1Button setImage:representer.image forState:UIControlStateNormal];
In your code, you are just instantiated another UIButton and setting the image on it. That is fine code but has nothing to do with the button in the xib.
This definitely assumes the IBOutlet is correctly wired up in Interface Builder. Here's how it should look:
Also, you need to be consistent in the button's image, versus the button's background image. They are two different things. If you have specified the "image" in Interface Builder, then do the same in your code, and vice versa with the backgroundImage.
Set a breakpoint at the line where you are changing the image and make sure cardOne is not nil. (or use an NSLog statement.) Broken outlet links are a very common reason that code doesn't work as expected.

Controlls are not displaying in interface builder?

i am facing a problem with IBOutlet,please Respected developers help me out, as i am fresher in iphone
for eg.
i declared buttons or controls with IBOutlet in .h file ,but many times interface builder is not showing the controls so i use to exit the application and reopen it again and i found it
so is there any thing i am missing.
thanks in advance...
Did you save your files? A mistake a lot of people new to iOS/Mac development don't realize is that Interface Builder will not show the IBOutlets unless you have saved the files first. So anytime you add an IBOutlet, make sure you save your header file before diving into Interface Builder.
How are you declaring your outlets? Also, what do you mean by "interface builder is not showing the controls"? If you have the following code:
#interface MyViewController : UIViewController {
IBOutlet UIButton *myButton;
IBOutlet UISlider *mySlider;
}
// Properties
// Methods
#end
In IB's connection inspector the outlets should show up and allow you to connect them to valid UI objects. Could you please clarify your issue?

iphone UIPickerView question

Here's the situation, I'm a newb at this - so I could be totally on the wrong path.
I am building an app with multiple views; in one of those views I need the user to select from a dropdown list (UIPickerView) --> for simplicity let's call that view "PC" which has PC.h and PC.m files.
Through IB I was able to drop a UIPickerView object to the PC view, and I initialize that "object" in my ViewController.h and ViewController.m files. Meaning, I'm able to load the view, populate data in the view, etc. etc.
My challenge/problem is - I want the UIPicker to be hidden until the user clicks a button on the PC view, then I want to show the UIPicker and hide it again once the user selects something from the menu.
I've searched and searched and can't find anything, so any help here is appreciated!
assuming that your UIPickerView instance (object) is called pv;
This is how your header-file may look like:
#interface YourViewController : UIViewController
{
IBOutlet UIPickerView *pv;
}
#property (nonatomic, retain) UIPickerView *pv;
#end
You then need to connect the pv-instance within the InterfaceBuilder to your Picker-View.
Trivial Approach:
somewhere in your viewDidLoad of the embedding view-controller:
pv.hidden = YES;
within the button action method (connected to your button-touch-up-instide event):
pv.hidden = NO;
within the action method of your "menu"
pv.hidden = YES;

iPhone: Proper use of View and View Controller

I've recently been doing a lot of Objective-C programming, and just got back into doing more iPhone development. I've done a lot of programming using MVC in other languages/frameworks, but I just want to make sure I'm using MVC properly in my iPhone Development.
I created a new iPhone Utility Application, which creates two views: MainView and FlipsideView. Both have a controller (FlipsideViewController and MainViewController) and XIB file of their own.
What I've been doing is putting the IBOutlet UIControl myControl variables in my MainView.h or FlipsideView.h files and then tying the controls in Interface Builder to those variables. Then I put any IBAction SomeAction myAction methods in the MainViewController.h and FlipsideViewController.h files and tying the events to those methods in Interface Builder.
This seems to be conceptually correct, but seems to cause problems. Say I have a button that when clicked it changes a label's text. So the Controller doesn't have a clue of what the variable name of the label is in the OnTouchUp event handler for my button. So I make a #property for it. But since the MainViewController.view property isn't of type MyView, I get warnings or errors whenever I try to access those properties from the view controller.
I am doing this correctly? Is there a better way to do this? If this is correct, how do I properly work with those variables without getting warnings or errors?
Thanks
Here's some code showing what I'm doing:
MainView.h
#import ...
#interface MainView : UIView
{
IBOutlet UILabel* label;
IBOutlet UIButton* button;
}
#property UILabel* label;
#property UIButton* button;
#end
MainViewController.m
-(void) buttonTouchUp:(id) sender
{
self.view.label.text = #"The button was pressed!"; //This gives error because label is not in the view structure or union
[self.view label].text = #"The button was pressed!"; //This gives a warning
}
I know I can cast the view to be of the right type, but that seems like a hack job.
I know I can live with the warning, but a warning says to me that I'm doing something that I probably shouldn't be doing. I don't believe the SDK would set me up to do have to do something like that all the time.
I must just be misunderstanding what the View is supposed to be and I'm using it incorrectly.
Most code I've seen and written myself keeps all the outlets and the actions on the controller. The button sends a message to the controller (via IBAction), then the controller updates the label (via IBOutlet). In other words, putting outlets on your views is unusual.
Views shouldn't be connected to other views unless they have some special relationship, like maybe back/forward buttons that target a UIWebView... but even then, you find out you need a controller to enable/disable the buttons as appropriate.