点击按钮时无法关闭模式视图 [英] Can't dismiss modal view when tap a button
问题描述
在"FirstViewController"中,我声明一个按钮,该按钮显示模式视图"InfoViewController".
In "FirstViewController" I declare a button which present the modal view "InfoViewController".
在"InfoViewController"中,我声明了一个带有"modalViewButton" UIButton的工具栏,该工具栏关闭了模态视图.但是确定" UIButton不起作用.我不知道为什么.
In "InfoViewController", I declare a toolbar with a "modalViewButton" UIButton which dismiss the modal view. But the "OK" UIButton doesn't work. I don't know why.
这是FirstViewController.h
Here's FirstViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
@interface FirstViewController : UIViewController
{
InfoViewController *infoViewController;
}
@property (nonatomic, retain) InfoViewController *infoViewController;
@end
这是FirstViewController.m
Here's FirstViewController.m
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize infoViewController;
- (IBAction)modalViewAction:(id)sender
{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[infoViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self
action:@selector(modalViewAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.leftBarButtonItem = modalBarButtonItem;
[modalBarButtonItem release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
这是InfoViewController.h
Here's InfoViewController.h
#import <UIKit/UIKit.h>
@interface InfoViewController : UIViewController
{
}
-(IBAction)infoDismissAction:(id)sender;
@end
这是InfoViewController.m
Here's the InfoViewController.m
#import "InfoViewController.h"
@implementation InfoViewController
- (IBAction)infoDismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *infoLabel = [[UILabel alloc] init];
infoLabel.frame = CGRectMake(50, 100, 100, 40);
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.text = @"About";
[self.view addSubview:infoLabel];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"OK"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(infoDismissAction:)];
UIBarButtonItem* infoTitle = [[UIBarButtonItem alloc] initWithTitle:@"About"
style:UIBarButtonItemStylePlain
target:self action:nil];
NSArray *barButtons = [[NSArray alloc] initWithObjects:flexibleSpace,flexibleSpace,infoTitle,flexibleSpace,doneButton,nil];
[toolBar setItems:barButtons];
[self.view addSubview:toolBar];
[toolBar release];
[infoTitle release];
[doneButton release];
[barButtons release];
[infoLabel release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
推荐答案
我将使用委托方法解决此问题.
I would solve this issue with a delegate method.
首先在您的modalViewController中创建一个协议
First make a protocol in your modalViewController
@protocol ModalViewDelegate <NSObject>
- (void)didDismissModalView;
@end
并在同一modalVC中设置一个委托属性:
And set a delegate property in the same modalVC:
id<ModalViewDelegate> dismissDelegate;
然后创建一个buttonActionMethod,该方法在modalVC中调用委托:
Then make a buttonActionMethod that calls the delegate in the modalVC:
- (void)methodCalledByButton:(id)sender
{
// Call the delegate to dismiss the modal view
[self.dismissDelegate didDismissModalView];
}
现在您的modalVC已经完成,您必须准备调用modalVC的mainVC: 您必须使MainViewController符合委托:
Now your modalVC is done you have to prepare the mainVC calling the modalVC: You have to make your MainViewController comform to the delegate:
@interface MainViewController : UIViewController <ModalViewDelegate>
在分配ModalViewController的地方,必须设置在modalViewController中创建的委托属性:
At the place you alloc your ModalViewController you have to set the delegate property you made in your modalViewController:
self.myModalViewController.dismissDelegate = self;
现在MainViewController会侦听委托,您唯一需要做的就是实现委托方法.
Now the MainViewController listens to the delegate and the only thing you need to do is implement the delegateMethod.
-(void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
现在,您的ModalVC将在按下按钮时关闭(至少在正确调用该方法的情况下)
Now your ModalVC will dismiss on a buttonpress (at least when you call the method properly)
希望这一切都有道理. 祝你好运.
Hope this all makes sense. Good luck.
这篇关于点击按钮时无法关闭模式视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!