3

私は2つのViewControllerを持っています.1つ目はTableViewで、2つ目はラベルが付いたボタンです。2番目のViewControllerのボタンをクリックすると、TableViewに戻って設定する必要があります

cell.detailTextLabel.text

ボタンのラベルのテキスト。

私が使用する最初のビューに戻るには:

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

しかし、2番目のビューからラベルを次のように設定する方法:

cell.detailTextLabel.text

初見で??????

4

3 に答える 3

2

2番目のView Controllerでプロトコルとデリゲートを定義します

@protocol SecondViewController;

@interface SecondViewController : UIViewController

@property (nonatomic, assign) id<SecondViewController> delegate;

@end


@protocol SecondViewController <NSObject>
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button;
@end   

次に、ボタンがタップされたときにデリゲートを呼び出します。

- (IBAction)buttonTapped:(UIButton *)sender
{
    // do somthing..

    // then tell the delegate about the button tapped
    [self.delegate secondViewController:self didTappedOnButton:sender];
}  

最初のView Controllerでプロトコルを実装します

@interface FirstViewController : UIViewController <SecondViewControllerDelegate>

2 番目のビュー コントローラーをプッシュするときは、最初のビュー コントローラーを 2 番目のデリゲートとして設定します。

- (void)someMethodThatPushTheSecondViewController
{
    SecondViewController *svc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:svc animated:YES];
    svc.delegate = self;
}  

ボタンがタップされたときに通知を受け取るデリゲート メソッドを実装します。

- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button
{
    // do somthing after button tapped
    // you can get the button title from button.titleLabel.text
}
于 2012-08-10T11:31:04.930 に答える
0

親クラスのメソッドまたはプロパティにアクセスするには、プロトコルを実装し、そのデリゲートを使用する必要があります。現在の(親)クラスで作成したクラスオブジェクトを使用して、子クラスのメソッド/プロパティにアクセスできます。しかし、子クラスから親クラスエンティティにどのようにアクセスしたいですか?はい、プロトコルを実装しています。

または、初心者の方法:ボタンをタップした後、必要な値をNSUserDefaultsに保存します。次に、親クラス(viewController 1)に移動するときに、viewWillAppearをイオン化して、保存された値を確認し、nilでない場合は、それを表示します。

于 2012-08-10T11:04:24.270 に答える