我在不使用委托协议的情况下呈现modalviewcontroller。但是想要使用委托协议来解雇modalviewcontroller。
基本上我正在推动像这样的modalviewcontroller
- (void)displayModalViewaction: (id) sender
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[Infoviewcontroller alloc] init];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
[_viewController release];
[navigationController release];
}
解雇执行此操作的modalview
在ModalViewController.h委托的协议和方法
@protocol ModalViewDelegate <NSObject>
-(void) dismissModalView:(UIViewController *) viewController;
@end
@interface Infoviewcontroller : UIViewController <ModalViewDelegate>
{
id<ModalViewDelegate> dismissDelegate;
}
@property (nonatomic, retain) id<ModalViewDelegate> dismissDelegate;
@end
在modalviewcontroller中。 m档
@synthesize dismissDelegate;
-(void) dismissModalView:(UIViewController *) viewController;
{
[self dismissModalViewControllerAnimated:YES];
}
@end
-(void) dismissView: (id)sender
{
[delegate dismissModalView:self];
}
-(void) dismissModalView:(UIViewController *) viewController;
{
[self.dismissModalViewController Animated:YES];
}
@end
但是点击完成按钮
时,它无法正常工作 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];
任何人都有任何线索,我的代码中缺少什么或我做错了什么。帮助将非常感激。
由于
答案 0 :(得分:1)
如果我错了,请纠正我,但是你不想在你的.h文件中定义_viewController并用[delegate dismissModalView:_viewController];
解雇它而不是解雇self,因为self不是一个视图控制器。
答案 1 :(得分:1)
我本来想评论,但没有足够的特权。无论如何AFAIK ......
一个人不会“推”一个模态VC ......它需要像这样'呈现'..
[navigationController presentModalViewController:_viewController animated:YES];
在调用[self dismissModalViewControllerAnimated:YES];
或者,如果你真的需要'推'它...那么你需要'弹出'才能回来!
希望有所帮助
答案 2 :(得分:1)
您正在推进导航的堆栈,因此没有显示任何模态,您需要将视图从堆栈中弹出:
[self.navigationController popViewControllerAnimated:YES];
而不是:
[self dismissModalViewControllerAnimated:YES];