将 UIViewController 作为 rootView 控制器的子视图控制器的模态视图呈现的问题
我有一个父视图控制器,它是 UIWindow 的 rootViewController 。这个 UIViewController 有两个子视图(添加了视图控制器)。当我点击右侧子视图控制器中的按钮时,它应该打开一个模式视图。当我尝试通过从右侧视图使用 [selfpresentModalView:vc] 来执行此操作时,它会折叠整个 UI。因此,我更改了代码,即我通过 AppDelegate 从parentViewController 呈现模式视图。因为appdelegate有parentView的实例。当我喜欢这个模态视图时,它会毫无问题地出现。
我的问题是,这是正确的方法吗?是否有关于呈现模态视图、该做什么和不该做什么的明确程序/文档?
我面临的另一个问题是,当我尝试在第一个模态视图上呈现另一个模态视图时,它不起作用。
请澄清我。
编辑:添加了代码示例来模拟该问题。 RootViewController 已添加到窗口中。 RightViewController是根视图控制器的子视图。当我单击右视图控制器上的按钮时,它将把模态视图控制器呈现为模态视图。问题就在这里。模态视图未出现 适当地。我希望这对你有帮助。
-- 提前致谢。 @杜莱
#import <UIKit/UIKit.h>
@class RightViewController;
@interface RootViewController : UIViewController {
UIView *bgView;
RightViewController *rightView;
}
@end
#import "RootViewController.h"
#import "RightViewController.h"
@implementation RootViewController
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
- (void) loadView
{
bgView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
rightView = [[RightViewController alloc] init];
[bgView addSubview:rightView.view];
self.view = bgView;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
// RightViewController.h
// ModalViewTester
//
//
#import <UIKit/UIKit.h>
#import "ModalViewController.h"
@interface RightViewController : UIViewController <ModalViewDelegate>{
UIView *rightView;
UIButton *button;
}
- (void) showModalView;
@end
#import "RightViewController.h"
@implementation RightViewController
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
- (void) loadView
{
rightView = [[UIView alloc] initWithFrame:CGRectMake(320, 40, 250, 250)];
rightView.backgroundColor = [UIColor yellowColor];
self.view = rightView;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 100, 30);
[button setTitle:@"Modal" forState:UIControlStateNormal];
[button addTarget:self action:@selector(showModalView) forControlEvents:UIControlEventTouchUpInside];
[rightView addSubview:button];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 50, 100, 50)];
label.text = @"Right View";
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0f];
[rightView addSubview:label];
self.view = rightView;
}
- (void) showModalView
{
ModalViewController *mc = [[ModalViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationFullScreen;
self.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:mc animated:YES];
[mc release];
}
- (void) closeView:(NSDictionary *)dict
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
#import <UIKit/UIKit.h>
@protocol ModalViewDelegate
-(void) closeView:(NSDictionary *) dict;
@end
@interface ModalViewController : UIViewController {
UIView *modalView;
UIButton *cancelButton;
id <ModalViewDelegate> delegate;
}
- (void) closeView:(id) sender;
@end
#import "ModalViewController.h"
@implementation ModalViewController
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
- (void) loadView
{
NSLog(@"Inside ModalViewController - loadView method");
modalView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
modalView.backgroundColor = [UIColor blueColor];
cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(150, 50, 70, 40)];
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
[modalView addSubview:cancelButton];
self.view = modalView;
}
- (void) closeView:(id) sender
{
[delegate closeView:nil];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
I have a parent view controller which is a rootViewController of the UIWindow. And this UIViewController has two sub views(viewcontrollers added). And when I tap a button in the right side subview-controller it should open a modal view. When I try to do this by using [self presentModalView:vc] from the right side view it collapses the whole UI. Hence I changed the code like, i.e I presented the modal view from the parentViewController through the AppDelegate. Because appdelegate has parentView's instance. When I do like this modal view appears without any problem.
My question, Is this the correct approach? Is there any clear procedure/documentation about presenting a modal view, do's and don't?.
And I am facing another problem is when I try to present another modal view over this first modalview it does not work.
Please clarify me.
Edited: added the code sample to simulate the issue. RootViewController is added to the window. RightViewController is the sub view of root view controller. When I click on the button on right view controller it will present the modal view controller as a modal view. Here is the problem. The modal view is not occur
properly. I hope this helps you.
--
Thanks in advance. @durai
#import <UIKit/UIKit.h>
@class RightViewController;
@interface RootViewController : UIViewController {
UIView *bgView;
RightViewController *rightView;
}
@end
#import "RootViewController.h"
#import "RightViewController.h"
@implementation RootViewController
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
- (void) loadView
{
bgView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
rightView = [[RightViewController alloc] init];
[bgView addSubview:rightView.view];
self.view = bgView;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
// RightViewController.h
// ModalViewTester
//
//
#import <UIKit/UIKit.h>
#import "ModalViewController.h"
@interface RightViewController : UIViewController <ModalViewDelegate>{
UIView *rightView;
UIButton *button;
}
- (void) showModalView;
@end
#import "RightViewController.h"
@implementation RightViewController
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
- (void) loadView
{
rightView = [[UIView alloc] initWithFrame:CGRectMake(320, 40, 250, 250)];
rightView.backgroundColor = [UIColor yellowColor];
self.view = rightView;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 100, 30);
[button setTitle:@"Modal" forState:UIControlStateNormal];
[button addTarget:self action:@selector(showModalView) forControlEvents:UIControlEventTouchUpInside];
[rightView addSubview:button];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 50, 100, 50)];
label.text = @"Right View";
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0f];
[rightView addSubview:label];
self.view = rightView;
}
- (void) showModalView
{
ModalViewController *mc = [[ModalViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationFullScreen;
self.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:mc animated:YES];
[mc release];
}
- (void) closeView:(NSDictionary *)dict
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
#import <UIKit/UIKit.h>
@protocol ModalViewDelegate
-(void) closeView:(NSDictionary *) dict;
@end
@interface ModalViewController : UIViewController {
UIView *modalView;
UIButton *cancelButton;
id <ModalViewDelegate> delegate;
}
- (void) closeView:(id) sender;
@end
#import "ModalViewController.h"
@implementation ModalViewController
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
- (void) loadView
{
NSLog(@"Inside ModalViewController - loadView method");
modalView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
modalView.backgroundColor = [UIColor blueColor];
cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(150, 50, 70, 40)];
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside];
[modalView addSubview:cancelButton];
self.view = modalView;
}
- (void) closeView:(id) sender
{
[delegate closeView:nil];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有任何代码很难真正理解您的问题,但如果您正在寻找文档,我认为这就是您需要查看的内容:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
It's hard to really grasp your problem without any code, but if it's documentation your looking for I think this is what you need to look at:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html