如何以编程方式将我的按钮连接到另一个视图控制器类
这是我的编程按钮
的代码UIBarButtonItem *yearButton= [[UIBarButtonItem alloc] initWithTitle:@"Year" style:UIBarButtonItemStyleBordered
target:self action:@selector(year:)];
-(void) year:(id)sender{
NSLog(@"Year button clicked");
//I don't know what should I write here to connect my button to UIViewController**
//when I added this line my process is terminated**
[self performSegueWithIdentifier:@"YearView" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString: @"YearView"]){
[segue.destinationViewController setTitle:@"YearView"];
}
}
在这里,您可以在故事板中看到我的观点: ![在此输入图像说明] [1]
修改 * 当我使用此方法时,我的进程终止 *
-(void)year:(id)sender{
// [self performSegueWithIdentifier:@"YearView" sender:self];
NSLog(@"Year button clicked");
YearView *yearVC = [[YearView alloc] initWithNibName:@"YearView" bundle:nil];
[[self navigationController] pushViewController:yearVC animated:YES]; // [yearVC release];
}
答案 0 :(得分:3)
试试这个:
YearView *year = [[YearView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:year animated:YES];
首先阅读教程是很好的: 从其他视图控制器呈现视图控制器 http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
答案 1 :(得分:1)
UIBarButtonItem *yearButton = [[UIBarButtonItem alloc] initWithTitle:@"Year"style:UIBarButtonItemStyleBordered target:self action:@selector(year:)];
self.navigationItem.rightBarButtonItem = yearButton;
-(void) year:(id)sender {
NSLog(@"Year button clicked");
YearViewcontroller *yearVC = [[YearViewcontroller alloc] initWithNibName:@"YearViewcontroller" bundle:nil];
[self.navigationController pushViewController:yearVC animated:YES];
[yearVC release];
}
答案 2 :(得分:0)
要将您的按钮与viewController中的方法相关联,您需要在界面设计器中设置IBOutlet(这是最简单的方法)。如果你仍然希望在另一个视图控制器中,你可以在viewDidLoad方法中设置另一个viewController的属性,这样另一个viewController也有一个对该按钮的引用,尽管远非理想。只有一个viewController应该管理按钮。