adr
adr

Reputation: 205

How to assign a name to every view in a storyboard

So I have an app where I use for the UI a storyboard with different views. In the first view, there's an if statment when I push a button. It decides if the screen has to show the next ViewController or other. I already know how to do this with .xibs, but no with storyboards.

Here's the code that doesn't work:

.h
{
IBOutlet UIView *one;
IBOutlet UIView *two;
}

.m

-(IBAction)decideNextView:(id)sender{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *NextView =[defaults objectForKey:@"TestDone"];
    if ([NextView isEqualToString:@""]) {
        self.view = one;
    }else if ([NextView isEqualToString:@"Done"]) {
        self.view = two;
    }else {
        self.view = one;
    }
}

When I run the app in my iPod Touch and I push the button that performs the IBAction, I get a black screen, but any error. Please help me!

Upvotes: 1

Views: 1669

Answers (1)

Mike Z
Mike Z

Reputation: 4111

The way you are supposed to access view elements in code is with the "Tag" item in the attributes inspector. The best way to do this is to set it to a number and then do #define kView1 0 so in code you don't need to remember what number you assigned to view1, you just use the constant.

Tag property

Where it says Tag here, you can set that to any number for each item in your storyboard or xib. Then in code you can say something like:

#define NAME_TAG 0
UIView *nameView = [self.view viewWithTag:NAME_TAG];

Upvotes: 1

Related Questions