Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

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.

Michael Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Michael Jones
Python Development Techdegree Graduate 37,766 Points

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.