1

如何以编程方式将我的按钮连接到另一个视图控制器类

这是我的编程按钮的代码

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

}
4

3 回答 3

3

试试这个:

YearView *year = [[YearView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:year animated:YES];

最好先阅读教程:Presing View Controllers from Other View Controllers http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

于 2012-07-23T10:26:33.933 回答
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];
}
于 2012-07-23T09:36:44.443 回答
0

要将您的按钮与 viewController 中的方法链接,您需要在界面设计器中设置一个 IBOutlet(这是最简单的方法)。如果您仍然希望在另一个视图控制器中使用它,您可以设置另一个视图控制器的属性,例如在 viewDidLoad 方法中,以便另一个视图控制器也具有对按钮的引用,尽管这远非理想。只有一个 viewController 应该管理该按钮。

于 2012-07-23T09:29:04.673 回答