完成按钮不会解雇modalviewcontroller

时间:2012-01-15 14:55:54

标签: iphone modalviewcontroller dismiss

当我点击完成按钮时,它不会解雇modalviewcontroller,在我的情况下它是infoviewcontroller。我应该在mainviewcontroller h和m文件中添加一些内容。

在Infoviewcontroller.h中

#import <UIKit/UIKit.h>

@protocol ModalViewDelegate <NSObject>

-(void) dismissModalView;

@end

@interface Infoviewcontroller : UIViewController <UITextViewDelegate>

{
id<ModalViewDelegate>dismissDelegate;
UITextView *textView;
UINavigationBar *navBar;

}

@property (nonatomic, retain) UITextView *textView;
@property (nonatomic, assign) UINavigationBar *navBar;
@property (nonatomic, assign) id<ModalViewDelegate>dismissDelegate;

@end

在Infoviewcontroller.m

#import "Infoviewcontroller.h"
#import <QuartzCore/QuartzCore.h>


@implementation Infoviewcontroller

@synthesize textView;
@synthesize navBar;
@synthesize dismissDelegate;



-(void)dealloc
{
[textView release];
[navBar release];
[super dealloc];
}

- (void) viewDidLoad
{
  [super viewDidLoad];

    UIButton* backButton = [UIButton buttonWithType:101]; 
    [backButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"Done" forState:UIControlStateNormal];

    // create button item
    UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    // add the button to navigation bar
    self.navigationItem.leftBarButtonItem = backItem;
    [backItem release];

 -(void)dismissView:(id)sender
{
    //Make the delegate close the modal
    [self.dismissDelegate dismissModalView];

}

当我点击完成按钮时,它不会解雇modalviewcontroller,在我的情况下它是infoviewcontroller。我应该在mainviewcontroller h和m文件中添加一些东西。如果我在mainviewcontroller文件中遗漏了一些东西,请帮帮我。

感谢您的帮助,

1 个答案:

答案 0 :(得分:3)

-(void)dismissView:(id)sender
{
       [self dismissModalViewControllerAnimated:YES];

}

但是如果你想在委托方法中解除控制器,那么首先必须在infoviewController中设置委托,然后在委托方法中使用infoviewController对象来解除它。