在我的 iPhone 应用程序中,我有两个 UIViewcontroler 类 firstView 和 secondView。
从 firstView 开始,我正在使用presentModalViewController:animated:方法展示 secondView。
问题是当我关闭 secondView 时,我想在 firstView 中隐藏按钮。
虽然它确实执行了 firstView 中的代码[button setHidden:YES];,但它仍然没有隐藏按钮。
有什么问题?
在我的 iPhone 应用程序中,我有两个 UIViewcontroler 类 firstView 和 secondView。
从 firstView 开始,我正在使用presentModalViewController:animated:方法展示 secondView。
问题是当我关闭 secondView 时,我想在 firstView 中隐藏按钮。
虽然它确实执行了 firstView 中的代码[button setHidden:YES];,但它仍然没有隐藏按钮。
有什么问题?
希望您已经声明了属性并合成了 IBOutlet button。
在 SecondViewController.h 中创建 FirstViewController 的对象和属性并合成它。
SecondViewController.h
@interface SecondViewController {
.
.
FirstViewController *firstView;
.
.
}
@property (nonatomic,strong) FirstViewController *firstView;
@end
第二视图控制器.m
@implementation SecondViewController
.
.
@synthesize firstView;
.
.
@end
现在,当您从 firstView 呈现模态视图时
第一视图控制器.m
-(IBAction)presentModalView {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondView.firstView = self;
[self presentModalViewController:secondView animated:YES];
}
现在在您关闭 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
第二视图控制器.m
@implementation SecondViewController
.
.
@synthesize delegate;
.
.
@end
现在,当您从 firstView 呈现模态视图时
第一视图控制器.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController<SecondViewControllerDelegate>
{
.
.
.
.
}
.
.
@end
第一视图控制器.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 中,只需添加此代码。
第二视图控制器.m
-(IBAction)dismissModalView {
[delegate hideButton];
[self dismissModalViewControllerAnimated:YES];
}
如果您需要更多帮助,请告诉我。
希望这可以帮助。
我会隐藏 viewDidDissapear 方法上的按钮,所以当用户调用模型时,按钮会隐藏。但这仅在您无法从 firstViewController 显示其他 viewController 时才有效。
我希望它有帮助!
我创建了两个控制器,FirstViewController 和 SecondViewController。每个控制器中都有一个按钮。
第一视图控制器.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController{
BOOL firstTime;// to hide button
}
@property (retain, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)buttonClicked:(id)sender;
@end
第一视图控制器.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
第二视图控制器.m
- (IBAction)secondButtonClicked:(id)sender {
[self dismissModalViewControllerAnimated:TRUE];
}
这对我有用。请试一试。