我想要完成的事情非常简单。我以编程方式将三个UIBarButtonItem添加到导航栏。然后我希望UIBarButtonItems以编程方式触发segues,我只想让segue执行。到目前为止,我的代码失败了,我已经花了6个半小时,而且我仍然不确定我在代码中缺少什么。
这是我的VC .h
@interface RGStemsViewController : UIViewController
@property (nonatomic, readonly) UIStoryboardSegue *stemsToAddGroup;
- (void)addGroupItemInvoked:(id)sender;
@end
和我的VC .m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
UIBarButtonItem *addGroupItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addGroupItemInvoked:)];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil];
UIBarButtonItem *optionsItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"optionsIconAlternative.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
NSArray *actionButtonItems = @[optionsItem, searchItem];
NSArray *groupButtonItem = @[addGroupItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
self.navigationItem.leftBarButtonItems = groupButtonItem;
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:153/256.0 green:204/256.0 blue:51/256.0 alpha:1.0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
- (void)addGroupItemInvoked:(UIBarButtonItem *)sender
{
[self performSeguewithIdentifier:@"stemsToAddGroup" sender:sender];
}
我通过右键单击视图控制器底栏上的视图控制器来连接segue,然后右键单击到目标视图,我想我需要使用prepareSegueWithIdentifier方法
显然我没有在我的@interface中声明选择器,或者至少那是我的错误所说的,这里似乎是什么问题?
完整的.m
@implementation RGStemsViewController
@synthesize stemsToAddGroup;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
UIBarButtonItem *addGroupItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addGroupItemInvoked:)];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil];
UIBarButtonItem *optionsItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"optionsIconAlternative.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
NSArray *actionButtonItems = @[optionsItem, searchItem];
NSArray *groupButtonItem = @[addGroupItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
self.navigationItem.leftBarButtonItems = groupButtonItem;
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:153/256.0 green:204/256.0 blue:51/256.0 alpha:1.0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
- (void)addGroupItemInvoked:(UIBarButtonItem *)sender
{
[self performSeguewithIdentifier:@"stemsToAddGroup" sender:self];
}
@end
答案 0 :(得分:1)
也许您需要为searchItem
和optionsItem
?