Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd random number of buttons to view
    text
    copied!<p>How can I do the following please?</p> <p>I have 2 UIViews - lets call them MainNavView and SubNavView. MainNavView has a set number (6) of buttons on it. </p> <p>Lets say the user clicks the 1st button on MainNavView, it will take them to SubNavView, and this view will have e.g. 4 buttons on it. But if they click the 2nd button on MainNavView, it will take them to SubNavView, but there will only be e.g. 2 buttons on it this time.</p> <p>So for each button on MainNav, it will have a SubNavView with a different set number of buttons on it. The number of buttons for each "SubNavView" is know.</p> <p>I am a bit stuck on how to do this. I have all my buttons connected to my pushButton action. Then depending on what button is pressed, I create a number of buttons and add to NSMutableArray.</p> <p>Then in prepareForSegue, I want to send this array to the SubNavView to populate it with buttons. Am I going in the right direction or not? </p> <p>Below is what I have in my MainNavView.m at the moment. I used the code in the switch-case to test if I could draw 2 buttons on screen in MainNavView (added the code to viewDidLoad initially) and this worked just fine.</p> <p>EDIT* OK looking at some examples, what I will need to do is get the tag of the button pressed on MainNavView and then do all the SubNav button creating on the viewDidLoad in the SubNavView instead based on the MainNav button tag id? Is this correct? If so, in viewDidLoad of SubNavView, how do I get access to what button was pushed? Not sure on this line in prepareForSegue.</p> <pre><code>[vc setSelectedButton:tagIndex]; </code></pre> <p>Updated to code to as follows in MainNavView.m:</p> <pre><code>-(IBAction)pushButton:(id)sender { [self performSegueWithIdentifier:@"segueToSubNav" sender:sender]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Make sure your segue name in storyboard is the same as this line if ([[segue identifier] isEqualToString:@"segueToSubNav"]) { // Get reference to the destination view controller SubNavViewController *vc = [segue destinationViewController]; // Get button tag number NSInteger tagIndex = [(UIButton *) sender tag]; // Pass any objects to the destination view controller here //[vc setSelectedButton:tagIndex]; } } enter code here </code></pre> <p>(Old code below)</p> <pre><code>#import "ViewController.h" #import "SubNavViewController.h" @interface ViewController () @end @implementation ViewController @synthesize buttons; @synthesize myButton1, myButton2; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(IBAction)pushButton:(id)sender { int theTag = ((UIView *) sender).tag; switch (theTag) { case 1: { NSLog(@"Getting Here"); buttons = [[NSMutableArray alloc] init]; // Button 1 myButton1 = [UIButton buttonWithType:UIButtonTypeCustom]; myButton1.tag = 1; // X, y, width, height myButton1.frame = CGRectMake(60, 60, 100.f, 100.f); UIImage *buttonImage1 = [UIImage imageNamed:@"button.png"]; [myButton1 setBackgroundImage:buttonImage1 forState:UIControlStateNormal]; [self.view addSubview:myButton1]; // Add targets and actions [myButton1 addTarget:self action:@selector(getButtons) forControlEvents:UIControlEventTouchUpOutside]; myButton1.tag = 0; // Button 2 myButton2 = [UIButton buttonWithType:UIButtonTypeCustom]; myButton2.tag = 1; // X, y, width, height myButton2.frame = CGRectMake(160, 160, 100.f, 100.f); UIImage *buttonImage2 = [UIImage imageNamed:@"button.png"]; [myButton2 setBackgroundImage:buttonImage2 forState:UIControlStateNormal]; [self.view addSubview:myButton2]; // Add targets and actions [myButton2 addTarget:self action:@selector(getButtons) forControlEvents:UIControlEventTouchUpOutside]; myButton2.tag = 0; // Add to array [buttons addObject:myButton1]; [buttons addObject:myButton2]; break; } case 2: NSLog(@"Where to Stay"); // Create buttons here break; case 3: NSLog(@"Where to Eat"); // Create buttons here break; default: break; } } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Make sure your segue name in storyboard is the same as this line if ([[segue identifier] isEqualToString:@"segueToSubNav"]) { // Get reference to the destination view controller SubNavViewController *vc = [segue destinationViewController]; // Pass any objects to the view controller here, like... [vc setMyObjectHere:buttons]; } } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload