1

プログラムでボタンを別のView Controllerクラスに接続するにはどうすればよいですか

ここに私のプログラムボタンのコードがあります

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

最初にチュートリアルを読むことをお勧めします: 他のビュー コントローラーからビュー コントローラーを提示する 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 を設定する必要があります (これが最も簡単な方法です)。別のView Controllerでそれが必要な場合は、たとえばviewDidLoadメソッドで別のviewControllerのプロパティを設定して、他のviewControllerにもボタンへの参照を持たせることができますが、それは理想的ではありません. 1 つの viewController のみがボタンを管理する必要があります。

于 2012-07-23T09:29:04.673 に答える