Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

make a button that goes to another viewcontroller (not using storyboarding)

Can someone explain to me using .xib on how to make a button push to another view controller (or another fresh screen I guess, my terminoligy isn't the greatest). Say I want to make a game that has a main screen that has a "settings" button. when I press this button I am taken to the settings screen. I don't want to use the storyboarding aspect, from what I understand it "isn't the best way to do it". Can you help? Thanks.

3 Answers

You want to change Views programmatically?

Yes. by pressing a button, we are taken to another "page" "screen" "xib" what have you and then have a "back button at the top" Maybe i'm thinking of this all incorrectly. I'm trying to figure out a way to do storyboarding wihtout using the storyboard because isn't that not the preferable way to make an app? idk.

I think so.

Okay, well the only way I know how to do it is to StoryBoard a little bit.

First create you'll need to add a 'Navigation Controller' by going to Editor >> Embedded In >> Navigation Controller. This will have to be linked to the UIView that is linked to the ViewController class.

Next, create a second UIView in StoryBoard then create a new set of 'Objective-C' classes. Then in the 'Utilities' tab then 'Identifier Inspector', link the new created View to the newly created classes by adding the name of the class to the 'Custom Class' section.

Next you'll need to create a segue using StoryBoards by right clicking one View and dropping it on the new View you created.

Click on the Segue link and in the 'Utilities' tab then 'Attribute Inspector' and set it a 'Identifier' name.

In the ViewController classes you could add the button in programmatically -

//.h file

@interface ViewController : UIViewController

@property (nonatomic, strong) UIButton *myButton;

  • (void) moveSegue;

@end

//.m file

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.

    //Create a rounded butotn. self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //Set the size and placement for the button. self.myButton.frame = CGRectMake(80.0f, 300.0f, 150.0f, 37.0f);

    //Display the text of the button to 'My Button'. [self.myButton setTitle:@"My Button" forState:UIControlStateNormal];

    //Once the button is clicked it'll request the method 'moveSegue'. [self.myButton addTarget:self action:@selector(moveSegue) forControlEvents:UIControlEventTouchUpInside];

    //Adds the button to my view. [self.view addSubview:self.myButton];

}

//This method is called once the button is pressed. Accessing the segue called 'infoSegue'.

  • (void) moveSegue{ [self performSegueWithIdentifier:@"infoSegue" sender:self]; }

I don't think I missed anything out. Hope this helps, give me a shout if you have anymore questions. I'll be glad to try and help you.