0

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)

CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

1

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.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
1

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.

Jamie
  • 5,090
  • 28
  • 26
  • I got the background to change but I cant get my other view to popup, the clicking and dragging from the new desired view's nib does not connect to anything in my main view controller – CQM Aug 14 '11 at 15:54
  • You don't want to connect anything to your new desired nib, you want to connect to something in your main nib that will trigger a method in your code to present the new nib on screen. The only thing you will need to do in your new nib is dismiss it and pass the information selected back to the main nib. – Jamie Aug 14 '11 at 16:36
  • `//present your new view on screen here` how do I make the new view appear, I tried `self.displayViewBackgroundChooser;` which is supposed to initialize. I don't see how the IBAction on my button will reference this view though – CQM Aug 14 '11 at 17:05
  • Do you have a custom UIViewController subclass for that view you created in IB? You need one. Say it's BackgroundChooserViewController. Then in the displayBackgroundChooser method, create one like this: BackgroundChooserViewController *bcvc = [[BackgroundChooserViewController alloc] init]; Now that you've created it, you can push it onscreen. [self presentModalViewController:bcvc animated:YES]; – Jamie Aug 15 '11 at 05:04