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

发布于 2024-12-09 13:08:06 字数 4951 浏览 0 评论 0原文

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

在“InfoViewController”中,我声明一个带有“modalViewButton”UIButton 的工具栏,它会关闭模式视图。但“确定”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

In "FirstViewController" I declare a button which present the modal view "InfoViewController".

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.

Here's FirstViewController.h

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

@interface FirstViewController : UIViewController 
{
    InfoViewController *infoViewController; 
}

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

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

Here's InfoViewController.h

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

转身以后 2024-12-16 13:08:06

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

首先在 modalViewController 中创建一个协议

@protocol ModalViewDelegate <NSObject>

 - (void)didDismissModalView;

@end

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

id<ModalViewDelegate> dismissDelegate;

然后创建一个在 modalVC 中调用该委托的 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 监听委托,你唯一需要做的就是实现 delegateMethod 。

-(void)didDismissModalView
{
    [self dismissModalViewControllerAnimated:YES];
}

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

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

I would solve this issue with a delegate method.

First make a protocol in your modalViewController

@protocol ModalViewDelegate <NSObject>

 - (void)didDismissModalView;

@end

And set a delegate property in the same modalVC:

id<ModalViewDelegate> dismissDelegate;

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

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>

At the place you alloc your ModalViewController you have to set the delegate property you made in your modalViewController:

self.myModalViewController.dismissDelegate = self;

Now the MainViewController listens to the delegate and the only thing you need to do is implement the delegateMethod.

-(void)didDismissModalView
{
    [self dismissModalViewControllerAnimated:YES];
}

Now your ModalVC will dismiss on a buttonpress (at least when you call the method properly)

Hope this all makes sense.
Good luck.

剩一世无双 2024-12-16 13:08:06

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

: 1) [selfmissModalViewControllerAnimated:YES];

2) 发送到 父视图控制器消息,当前模态视图应该被关闭并发送对该视图的引用。

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

You can only dismiss currently displayed modal view, so in your method infoDismissAction: you should do one of following

1) [self dismissModalViewControllerAnimated:YES];

2) Send to parent view controller message that current modal view should be dismissed and send reference to that view.

Second approach is better as it is more safe.

心安伴我暖 2024-12-16 13:08:06

在您的 -infoDismissAction 中尝试调用 [selfmissModalViewControllerAnimated:YES];

In your -infoDismissAction try to call [self dismissModalViewControllerAnimated:YES];

晨光如昨 2024-12-16 13:08:06

这里还有 iPhone 和 iPad 模型视图的最佳示例代码。

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

从此处下载示例代码。

Here the best sample code for the model view for iphone and ipad also.

The popups have a number of configurable items. They can be animated to either slide or popup onto the display. Once visible, they can be dismissed either by tapping the screen or after a programmed delay. The background and text colors can also be adjusted however you like.

Download the sample code from here.

小鸟爱天空丶 2024-12-16 13:08:06

当前的答案已被弃用。这是更新后的代码:

[self dismissViewControllerAnimated:NO completion:nil];

The current answers are deprecated. Here is the updated code:

[self dismissViewControllerAnimated:NO completion:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文