1

私の iPhone アプリには、firstView と secondView の 2 つの UIViewcontroler クラスがあります。

firstView から、メソッドを使用して secondView を提示していpresentModalViewController:animated:ます。

問題は、secondView を閉じるときに、firstView のボタンを非表示にしたいことです。

コードは firstView[button setHidden:YES];で実行されますが、それでもボタンは非表示になりません。

何が間違っている可能性がありますか?

4

3 に答える 3

2

プロパティを宣言して IBOutlet を合成したことを願っていbuttonます。

SecondViewController.h とプロパティで FirstViewController のオブジェクトを作り合成します。

SecondViewController.h

@interface SecondViewController {
.
.
FirstViewController *firstView;
.
.
}
@property (nonatomic,strong) FirstViewController *firstView;

@end

SecondViewController.m

@implementation SecondViewController 
.
.
@synthesize firstView;
.
.
@end

firstView からモーダル ビューを表示すると、

FirstViewController.m

-(IBAction)presentModalView {
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondView.firstView = self;
    [self presentModalViewController:secondView animated:YES];
}

SecondViewController を閉じる SecondViewController で、このコードを追加するだけです。

SecondViewController.m

-(IBAction)dismissModalView {
    [self.firstView.button setHidden:YES];
    [self dismissModalViewControllerAnimated:YES];
}

編集:

次のリンクを参照してください。

Objective-C の @interface での @protocol 実装

EDIT-2: プロトコル実装あり

SecondViewController.h

@protocol SecondViewControllerDelegate <NSObject>
@required
    - (void)hideButton;
@end

@interface SecondViewController {
.
.
id <SecondViewControllerDelegate> delegate;
.
.
}
@property (retain) id delegate;

@end

SecondViewController.m

@implementation SecondViewController 
.
.
@synthesize delegate;
.
.
@end

firstView からモーダル ビューを表示すると、

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController<SecondViewControllerDelegate>
{
.
.
.
.
}
.
.
@end

FirstViewController.m

-(IBAction)presentModalView {
    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    secondView.delegate = self;
    [self presentModalViewController:secondView animated:YES];
}

#pragma mark - SecondViewController Delegate

- (void)hideButton
  {
      [self.button setHidden:YES]; //Here button is UIButton you want to hide when second view is dismissed.
  }

SecondViewController を閉じる SecondViewController で、このコードを追加するだけです。

SecondViewController.m

-(IBAction)dismissModalView {
    [delegate hideButton];
    [self dismissModalViewControllerAnimated:YES];
}

これについてさらにサポートが必要な場合はお知らせください。

お役に立てれば。

于 2012-04-11T09:19:07.703 に答える
0

FirstViewController と SecondViewController の 2 つのコントローラーを作成しました。そして、各コントローラーに 1 つのボタン。

FirstViewController.h

#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController{
   BOOL firstTime;// to hide button 
}
@property (retain, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)buttonClicked:(id)sender;
@end

FirstViewController.m

-(void)viewWillAppear:(BOOL)animated{
   if (firstTime) {
     [myButton setHidden:TRUE];
   }
}
- (IBAction)buttonClicked:(id)sender {
   firstTime   =   TRUE;
   SecondViewController *secondController  =   [[SecondViewController alloc]init];
   [self presentModalViewController:secondController animated:TRUE];
   [secondController release];
}

SecondViewController.h

@interface SecondViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIButton *secondButton;
- (IBAction)secondButtonClicked:(id)sender;
- (IBAction)secondButtonClicked:(id)sender;
@end

SecondViewController.m

- (IBAction)secondButtonClicked:(id)sender {
   [self dismissModalViewControllerAnimated:TRUE];
}

これは私にとってはうまくいきました。ぜひお試しください。

于 2012-04-11T10:53:56.317 に答える
0

viewDidDissapear メソッドのボタンを非表示にすると、ユーザーがモデルを呼び出すとボタンが非表示になります。ただし、これは、最初のViewControllerから他のviewControllerを表示できない場合にのみ機能します。

役に立てば幸いです!!

于 2012-04-11T07:45:57.613 に答える