2011-10-14 53 views
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 

回答

3

我會解決這個問題有一個委託方法。

首先要在你的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完成了,你必須準備mainVC調用t他modalVC: 你必須讓你的MainViewController comform的委託:

@interface MainViewController : UIViewController <ModalViewDelegate> 

在這個地方你的Alloc您ModalViewController你必須設置你在modalViewController作出委託屬性:

self.myModalViewController.dismissDelegate = self; 

現在MainViewController監聽委託,唯一需要做的就是實現delegateMethod。現在

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

您ModalVC將解僱在buttonpress(至少當你調用正確的方法)

希望這一切是很有意義的。 祝你好運。

1

只能辭退當前顯示模式的看法,所以在你的方法infoDismissAction:你應該做一個下列

1)[self dismissModalViewControllerAnimated:YES];

2)發送到parent view controller消息的,目前的模式的看法應該被解僱,併發送參考該觀點。

第二種方法更好,因爲它更安全。

+0

是的,這是一個更好的答案。 @lavitanien,如果您想查看第二個建議,請研究Xcode附帶的「實用工具」模板中的代碼。 – phi

+0

非常感謝! – lavitanien

0

在你-infoDismissAction嘗試調用[self dismissModalViewControllerAnimated:YES];

0

這裏的iPhone和iPad的模型視圖的最佳示例代碼也。

彈出窗口有許多可配置的項目。他們可以動畫,以滑動或彈出顯示。一旦可見,可以通過點擊屏幕或經過編程的延遲來解除它們。無論你喜歡,背景和文字顏色也可以進行調整。

下載sample code from here.

0

目前的答案是不推薦使用。這裏是更新的代碼:

[self dismissViewControllerAnimated:NO completion:nil];