Changing the Image of a UIButton at Runtime - iphone

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.

Related

Change a UIBarButtonItem's style to a BarButtonSystemItem

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.

How can I set the background image of multiple buttons in a certain view from a different view?

Basically, I cannot figure out how to change the background image. I have searched and searched and just cannot seem to find it. Anyone have any ideas? Thanks!
Update
Here is the code I use to show the View:
SettingsViewController *settingsView = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:settingsView animated:YES];
If anyone needs anything else to help out I'll do my best! Thanks!
Passing a messages between different views can be done by direct call of the methods (not a good architectural solution but maybe not critical for a small projects) or with an event driven model as described in an answer above.
As for binding of a collection of objects and processing the afterward I recommend to review IBOutletCollection keyword that allows to bind multiple objects fro an InterfaceBuilder to a property with a type like NSArray.
Property declaration will look like following:
#property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;
The code to change the background for all the buttons will look like following:
UIImage *backgroundImage = [UIImage imageNamed:#"background.png"];
for(UIButton *button in buttons) {
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
}
You use setBackgroundImage:forState: to set the image.
Regarding the different views part, it depends on how you wrote your code. If the view with the buttons is controlled by the different view (by creating an instance and using addSubview:) then you can call it directly by using instanceName.buttonName (as long as you declare it as a property -thanks fichek).
If you don't manually add the view, instead through IB, you can have the button that controls the other button's image point to the IBAction in that class.
If neither of those options work you can always use NSNotificationCenter.

Add subview not working?

I have two xib files:
MainView.xib and DetailView.xib
Both are controlled by MainViewController. MainView.xib loads when the app first opens, but if a user clicks on a button, the app loads DetailView.xib as a subview.
DetailView should load because I made an IBOutlet in the MainViewController to the view in the DetailView.xib file.
I am trying to use the addSubview command, but for some reason it is not actually executing the command. It will go through the command, but nothing will actually change. Here is the command:
[self.view addSubview:myDetailView]
where myDetailView is the IBOutlet
What is wrong with this setup?
Thanks for the help.
EDIT:
MainViewController.h (left generated code out):
IBOutlet UIView *myDetailView;
#property (nonatomic, retain) IBOutlet UIView *myDetailView;
MainViewController.m:
#synthesize myDetailView;
NSLog myDetailView before you add the subview, and if it returns "(null)" then the myDetailView has not been initialised. Make sure that you have connected the view in Interface Builder.
I bet myDetailView is nil.
It may be an IBOutlet, but it has to be connected. And the XIB in which you connected the IBOutlet should obviously be loaded.
How did you load the DetailView.xib in your code? Did you use loadNibNamed:owner:options:?

Iphone SDK App UIImageView

I'm a beginner to Iphone Development :)
I am trying to make a button change an image. So I have my button
- (IBAction)myButton {
myUIImageView.image = [UIImage imageNamed:#"myPhoto.png"];
}
I created a UIImageView in IB and I named the label and the name of this 'myUIImageView' but XCode is telling me that it's undeclared. So my question is how do I associate this UIImageView with myUIImageView. Or perhaps how do I reference this UIImage in my myButton IBAction?
Any advice would help, thanks alot!
In your .h, you need this ivar between the curly braces
UIImageView* myUIImageView;
And after the close and before the #end, you need
#property(retain, nonatomic) IBOutlet UIImageView* myUIImageView;
and in the .m, after the #implementation line
#synthesize myUIImageView;
(release in your dealloc and viewDidUnload)
Now,
Open up Interface Builder for the .xib for this view controller
Click on File's Owner icon in the Document dialog
Bring up the Inspector
Go to the connections tab
You should see an outlet named myUIImageView with a circle next to it
Click and drag the circle to the UIImageView in your view (this connects the outlet to the view)
Save, close, rebuild
how to connect the UIImageView in interface builder with the outlet created in xcode named myUIImageView:
close interface builder and open up xcode. Heres what you need to write correctly in the following two files.
in XCode
.h file
#interface FirstViewController : UIViewController {
IBOutlet UIImageView *myUIImageView;
}
#property(retain, nonatomic) IBOutlet UIImageView *myUIImageView;
#end
.m file
after implementation you write
#synthesize myUIImageView;
release in your dealloc and viewDidUnload.
save the file in xcode then open up the xib file that is connected to the .h and .m files. For example i have firstViewcontroller.h and firstViewController.m then i have a .xib file called firstView.xib.
in Interface Builder
Now on the view drag a UIImageView and then in the document dialog you will see the file owners icon.
click on that and press CMD+2 to open up the inspector. Go to the connections tab and there will be an outlet named myUIImageView that we created in xcode. next to it is a circle which you click and drag to your UIImageView. This will connect the outlet in xcode with the imageview in interface builder.
Now save the file. close interface builder and rebuild your project.
Thats the first question answered once you ellaborated on question two i will help you with that.
Let me know if you need any more help.
PK

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.