点击按钮时无法关闭模式视图

Posted

技术标签:

【中文标题】点击按钮时无法关闭模式视图【英文标题】:Can't dismiss modal view when tap a button 【发布时间】:2011-10-14 07:25:13 【问题描述】:

在“FirstViewController”中,我声明了一个显示模式视图“InfoViewController”的按钮。

在“InfoViewController”中,我声明了一个带有“modalViewButton”UIButton 的工具栏,它可以关闭模​​态视图。但是“OK” UIButton 不起作用。我不知道为什么。

这里是 FirstViewController.h

#import <UIKit/UIKit.h>
#import "InfoViewController.h"

@interface FirstViewController : UIViewController 

    InfoViewController *infoViewController; 


@property (nonatomic, retain) InfoViewController *infoViewController;
@end

这里是 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

#import <UIKit/UIKit.h>     
@interface InfoViewController : UIViewController 



-(IBAction)infoDismissAction:(id)sender;
@end

这是 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

【问题讨论】:

【参考方案1】:

我会用委托方法解决这个问题。

首先在你的 modalViewController 中创建一个协议

@protocol ModalViewDelegate <NSObject>

 - (void)didDismissModalView;

@end

并在同一个modalVC中设置一个委托属性:

id<ModalViewDelegate> dismissDelegate;

然后在modalVC中做一个调用delegate的buttonActionMethod:

- (void)methodCalledByButton:(id)sender 

    // Call the delegate to dismiss the modal view
    [self.dismissDelegate didDismissModalView];

现在你的 modalVC 已经完成了,你必须准备调用 modalVC 的 mainVC: 你必须让你的 MainViewController 符合委托:

@interface MainViewController : UIViewController <ModalViewDelegate>

在您分配 ModalViewController 的地方,您必须设置您在 modalViewController 中创建的委托属性:

self.myModalViewController.dismissDelegate = self;

现在 MainViewController 监听委托,您唯一需要做的就是实现委托方法。

-(void)didDismissModalView

    [self dismissModalViewControllerAnimated:YES];

现在您的 ModalVC 将在按下按钮时关闭(至少在您正确调用该方法时)

希望这一切都有意义。 祝你好运。

【讨论】:

【参考方案2】:

您只能关闭当前显示的模式视图,因此在您的方法 infoDismissAction: 中您应该执行以下操作之一

1)[self dismissModalViewControllerAnimated:YES];

2) 向parent view controller 发送消息,指出当前模态视图应该被关闭并发送对该视图的引用。

第二种方法更好,因为它更安全。

【讨论】:

是的,这是一个更好的答案。 @lavitanien,如果您想查看第二条建议的实际效果,请研究 Xcode 附带的“实用程序”模板中的代码。【参考方案3】:

在您的-infoDismissAction 中尝试致电[self dismissModalViewControllerAnimated:YES];

【讨论】:

【参考方案4】:

这里也是 iphone 和 ipad 模型视图的最佳示例代码。

弹出窗口有许多可配置的项目。它们可以被动画化以滑动或弹出到显示器上。一旦可见,它们可以通过点击屏幕或在编程延迟后关闭。背景和文字颜色也可以随意调整。

下载sample code from here.

【讨论】:

【参考方案5】:

不推荐使用当前答案。这是更新的代码:

[self dismissViewControllerAnimated:NO completion:nil];

【讨论】:

以上是关于点击按钮时无法关闭模式视图的主要内容,如果未能解决你的问题,请参考以下文章

从标签栏视图中的视图中关闭模式

导航按钮无法关闭模式

在弹出视图之外的任何地方点击时关闭弹出视图,包括按钮、文本字段等

出现后无法关闭视图控制器

关闭模式视图时更新 UILabels

结合点击关闭键盘、键盘避免视图和提交按钮