0

Hey I'm having a lot of trouble with this idea I have. How do I make buttons that go to a different screen but on the CODE?

Basically depending on a value that may or may not be different for every user (Let's say the value is in x already) it will make a list of buttons...

When you click the list of buttons, they each individually go to a different screen.

How do I do this? I know how to on storyboard.. but code wise?

Edit: I SHOULD note that for the buttons specifically I have this and it is working in the code (didn't use storyboard):

        UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(50, 200, 200, 50);
        [btn setTitle:@"Button 1" forState:UIControlStateNormal];
        [self.view addSubview:btn];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3159537
  • 61
  • 1
  • 12
  • Do not put tags in the title and use proper tags for your question. This is Objective-C code, not C code. And the "new" tag clearly states not to use it. – rmaddy Mar 25 '14 at 22:56
  • yeah i know this is objective c code and not c, but stackoverflow told me i couldn't use the word objective. ah well. – user3159537 Mar 26 '14 at 00:01
  • your question is you have a list of buttons and on clicking those buttons depending on button clicked it hold go to respective screen? – Charan Giri Mar 26 '14 at 03:24
  • Based on what a value is (can be 4000 different values - depending on what user inputs) <-- i already have the code so the value is a variable. It will create a list of buttons and upon clicking those buttons they each will link to their respective screen. – user3159537 Mar 26 '14 at 07:12

2 Answers2

0

First add a target to your button:

  //Add target to your button, to call a method that presents the next ViewController:
    UIButton *yourButton = [[UIButton alloc]init];
    [yourButton addTarget:self action:@selector(goToNextPageMethod) forControlEvents:UIControlEventTouchUpInside];

User one of the two, case you are using a UINavigation controller or not, place them in the method you are calling when the button is pressed (the target)

    //Normal presentation
    YourViewController *vc = [[YourViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
    //Navgiation bar
    YourViewController *vc = [[YourViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

Hope this helps

MCMatan
  • 8,623
  • 6
  • 46
  • 85
0

You can attach an action to your button by doing the following:

UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(50, 200, 200, 50);
[btn setTitle:@"Button 1" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(someAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

and then in your someAction method, you push the view modally or via your navigation controller

- (void)someAction
{
    //init your view controller here....
    YourViewController *vc = [[YourViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
MikeT
  • 394
  • 2
  • 13
  • But how do I put in the someAction method to go to a new screen? and how do I decide what goes there in the code (Not using storyboard) ? – user3159537 Mar 25 '14 at 23:40
  • in the someAction method you basically init a new viewcontroller which is the "screen" you want to show (see @PiratM 's answer on how to do this). And then you push that new viewcontroller. Edited my answer to include the someAction method - hope that helps. – MikeT Mar 25 '14 at 23:45
  • It does actually. Thank you a lot. I know you're going to think this is a really dumb question but it would help me a lot. What's the difference between a - and a + before the (void)? In the default code they use +... and also the compiler is stating someAction is undeclared.. how do I declare it? Thank you so much! – user3159537 Mar 25 '14 at 23:56
  • glad that it helps! the + is a class method and - is an instance method. I think this post answers it very well. http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods – MikeT Mar 25 '14 at 23:59
  • The above code seems to be fine for me. Make sure the selector is not someAction: and it is someAction – MikeT Mar 26 '14 at 00:05
  • Thanks Mike, but I still am receiving the error of use of undeclared identifer ???? The selector being undeclared is actually something common in iOS and I just disabled it but for the void well thats what it's saying.. its undeclared. – user3159537 Mar 26 '14 at 00:49