我怎么能这样做?
我有2个UIViews - 让我们称之为MainNavView和SubNavView。 MainNavView上有一组(6)个按钮。
让我们说用户点击MainNavView上的第一个按钮,它会将它们带到SubNavView,这个视图将有例如它上面有4个按钮。 但是,如果他们点击MainNavView上的第二个按钮,它将把它们带到SubNavView,但只有例如这次有两个按钮。
因此,对于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
答案 0 :(得分:0)
您可以在property
课程中添加SubNavView
,其中会按住按钮的标记。然后,当您创建SubNavView
的实例时,在显示该属性之前设置该属性。您还需要将所选标记保存在MainNavView
的ivar中,以便在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
的{{1}}时,您可以使用viewDidLoad
子句创建按钮:
if-else
希望这有帮助!