0

请问我该怎么做?

我有 2 个 UIViews - 我们称它们为 MainNavView 和 SubNavView。MainNavView 上有一组 (6) 个按钮。

假设用户单击 MainNavView 上的第一个按钮,它会将他们带到 SubNavView,并且该视图将具有例如 4 个按钮。但是如果他们点击 MainNavView 上的第二个按钮,它会将他们带到 SubNavView,但这次只有 2 个按钮。

因此,对于 MainNav 上的每个按钮,它都会有一个 SubNavView,上面有不同数量的按钮。每个“SubNavView”的按钮数量是已知的。

我有点坚持如何做到这一点。我的所有按钮都连接到我的 pushButton 操作。然后根据按下的按钮,我创建了一些按钮并添加到 NSMutableArray。

然后在 prepareForSegue 中,我想将此数组发送到 SubNavView 以使用按钮填充它。我是否朝着正确的方向前进?

以下是我目前在 MainNavView.m 中的内容。我使用 switch-case 中的代码来测试我是否可以在 MainNavView 的屏幕上绘制 2 个按钮(最初将代码添加到 viewDidLoad)并且效果很好。

编辑*好的,看看一些例子,我需要做的是获取在 MainNavView 上按下的按钮标签,然后根据 MainNav 按钮标签 ID 在 SubNavView 中的 viewDidLoad 上创建所有 SubNav 按钮?这个对吗?如果是这样,在 SubNavView 的 viewDidLoad 中,我如何访问按下的按钮?在 prepareForSegue 中不确定这一行。

[vc setSelectedButton:tagIndex];

在 MainNavView.m 中更新为如下代码:

-(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

(以下旧代码)

#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
4

1 回答 1

0

property您可以在您的类中添加一个来SubNavView保存按下按钮的标签。然后,当您创建一个实例时SubNavView,在显示它之前设置该属性。您还需要将所选标签保存在 ivar 中,MainNavView以便您可以将所选标签设置为SubNavView.

这是一个例子:

SubNavViewController.h

@interface SubNavViewController : UIViewController

@property (nonatomic, strong) NSInteger tagSelected;

// The rest of your class declaration

@end

MainNavViewController.m

@interface MainNavViewController ()

// This is the ivar to save the selected tag
@property (nonatomic, strong) NSInteger tagSelected;

@end

@implementation MainNavViewController ()

// Your class code

-(IBAction)pushButton:(id)sender
{
    int theTag = ((UIView *) sender).tag;

    // save the tag that was selected for later use
    sefl.tagSelected = theTag; 

    switch (theTag)
    {
        // The rest of your switch here...
    }
}

- (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];

        // Tell SubNavView which button was pressed...
        vc.selectedTag = self.selectedTag;

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:buttons];
    }
}

@end

之后,当SubNavView'sviewDidLoad被调用时,您可以使用一个if-else子句来创建您的按钮:

-(void)viewDidLoad {

    [super viewDidLoad];

    if (self.selectedTag == 1) {

        //Do something...
    } 
    else if (self.selectedTag == 2) {

        //Do something else...
    } 
    // etc...
}

希望这可以帮助!

于 2013-10-23T23:07:17.233 回答