开发者

ios change background on button click

开发者 https://www.devze.com 2023-03-27 19:34 出处:网络
I so far only have the interface builder layout I\'m not clear on the syntax to reference all开发者_开发百科 of these items from the layout

I so far only have the interface builder layout

I'm not clear on the syntax to reference all开发者_开发百科 of these items from the layout

I know that IBOutlet has to be used somewhere, but I need a bit more handholding on what this objective C is doing. Nothing I've read tells me exactly why some declarations start with + and others with -

What I want to do is click a button in my layout, have a modal view pop up and change the background on the entire layout.

so the first step is referencing all these items I've made in the nib. help? (or post a link to more intuitive tutorials that you know about)


So you probably want to create an IBOutlet for your background view. Maybe it's a UIImageView that you can set it's image property based on what the user selects in the modal view. For this you would just declare the UIImageView you have in your IB file

UIImageView *imageView;

and then declare it as a property

@property (nonatomic,retain)IBOutlet UIImageView *imageView;

and synthesize it in your .m file

@synthesize imageView;

Don't forget to release it if you're not using ARC.

Then you can open up interface builder and if you click on your view controller File's Owner and go to the connections inspector you will see there is a new connection there for imageView. Just drag that connection over to your UIImageView in the IB file and that's it. You now have a reference in your code that connects to your UIImageView in IB.

That will allow you to set the UIImageView in your code by typing something like

self.imageView.image = [UIImage imageNamed:theNameTheUserJustPicked];

In order to get the modal view, you need an IBAction to trigger a method in your code so declare one like this in your .h file of your main nib.

- (IBAction)displayViewBackgroundChooser;

and then define it in your .m file.

- (IBAction)displayViewBackgroundChooser {
    //present your new view on screen here
}

Then go back to interface builder and click on the File's Owner again. You should see it there in the connections inspector and then you can connect it to a button, for example, that would trigger that method.

Hope this helps to clear things up a bit on IBOutlets and IBActions.


You can make your UI elements created in IB interact with your code by means of IBOutlets and IBActions.

In your case, I would associate an action to the button, so that it is fired when the button is clicked; the action would open a modal view, and you could change the background of that view in the viewDidLoad method of the associated controller.

Here you find a video tutorial about adding an outlet. And here, the same about actions.

About your doubt on + and -, - identifies a normal method defined in a class; + defines a class method, i.e., a method that you can call on the class directly, without having to instantiate it first. Have a look at this S.O. article for more.

0

精彩评论

暂无评论...
验证码 换一张
取 消