从模态视图中隐藏另一个视图中的按钮

时间:2012-04-11 05:43:19

标签: iphone objective-c uibutton presentmodalviewcontroller

在我的iPhone应用程序中,我有两个UIViewcontroler类firstView和secondView。

从firstView,我使用presentModalViewController:animated:方法呈现secondView。

问题是当我解雇secondView时,我想在firstView中隐藏按钮。

虽然它确实执行firstView [button setHidden:YES];中的代码,但它仍然没有隐藏按钮。

可能出现什么问题?

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分: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];
}

修改

请参阅此链接:

@protocol implementation in @interface in Objective-C

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

如果您需要更多帮助,请与我们联系。

希望这有帮助。

答案 1 :(得分:0)

我会隐藏viewDidDissapear方法上的按钮,因此当用户调用模型时按钮会隐藏。但这只有在你无法从firstViewController中显示其他viewControllers时才有效。

我希望它有所帮助!!

答案 2 :(得分:0)

我创建了两个控制器,FirstViewController和SecondViewController。 每个控制器都有一个按钮。

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

这对我有用。请试一试。