我有這個類來創建按鈕導航欄:另一個類中的動作按鈕?
NavigationBar.m
+ (void)styleNavBar :(UIView*) view withColor:(UIColor*)color {
// 1. hide the existing nav bar
//[self.navigationController setNavigationBarHidden:YES animated:NO];
// 2. create a new nav bar and style it
UINavigationBar *newNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20.0, CGRectGetWidth(view.bounds), 44.0)];
newNavBar.barTintColor = color;
//newNavBar.shadowImage = nil;
// 3. add a new navigation item w/title to the new nav bar
UINavigationItem *newItem = [[UINavigationItem alloc] init];
//newItem.title = @"AmisViewController";
UIBarButtonItem *myAddButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(myAddAction:)];
UIBarButtonItem *myCoolButton = [[UIBarButtonItem alloc] initWithTitle:@"Cool!" style:UIBarButtonItemStyleDone target:self action:@selector(myCoolAction:)];
NSArray *myButtonArray = [[NSArray alloc] initWithObjects:myCoolButton, myAddButton, nil];
newItem.leftBarButtonItems = myButtonArray;
[newNavBar setItems:@[newItem]];
// 4. add the nav bar to the main view
[view addSubview:newNavBar];
}
和2事件:
-(IBAction)myAddAction:(id)sender{
NSLog(@"myAddButton");
}
-(IBAction)myCoolAction:(id)sender{
NSLog(@"myCoolButton");
}
我的問題是,當我打電話給我的梅索德styleNavBar在viewcontrollers我的應用程序崩潰+[NavigationBar myCoolAction:]: unrecognized selector sent to class
我的問題是我怎麼可以添加methodes myAddAction和myCoolButton?
該代碼看起來不錯。澄清一下,那些方法存在於那個類中? – trojanfoe