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

问题描述 投票:1回答:3

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

从firstView,我正在使用presentModalViewController:animated:方法显示secondView。

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

尽管它确实在firstView [button setHidden:YES];中执行代码,但仍不隐藏按钮。

可能有什么问题?

iphone objective-c uibutton presentmodalviewcontroller
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];
}

编辑:

请参阅此链接:

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

让我知道您是否需要更多帮助。

希望这会有所帮助。

我会在viewDidDissapear方法上隐藏按钮,因此当用户调用模型时,按钮会隐藏。但这仅在您无法从firstViewController中显示其他viewController时起作用。

希望对您有帮助!

我创建了两个控制器,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];
}

这对我有用。请尝试一下。


0
投票

我会在viewDidDissapear方法上隐藏按钮,因此当用户调用模型时,按钮会隐藏。但这仅在您无法从firstViewController中显示其他viewController时起作用。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.