gpt4 book ai didi

ios - 在模态视图 Controller 之间切换

转载 作者:行者123 更新时间:2023-12-01 19:24:12 26 4
gpt4 key购买 nike

我的应用程序允许用户在两​​个不同的模态视图 Controller 之间切换(用于两种不同样式的数据输入)。下面的代码曾经可以工作(在 iOS 4.3 和更早版本中):

    UIViewController * parent = current.parentViewController;
[current dismissModalViewControllerAnimated:NO];
svc.modalPresentationStyle = UIModalPresentationFormSheet;
[parent presentModalViewController:svc animated:NO];
[svc release];

但不再(在 iOS 5 中)-“当前” View Controller 关闭,但“svc”不显示。

知道为什么它坏了(即我做错了什么)?
知道如何“正确”地做到这一点(以便它适用于 5.0 以及 4.3 及更早版本)?

最佳答案

Jeff Hay 的评论完全正确,除了一件事。您应该在 -viewDidAppear: 中执行此操作最初呈现第一个模态视图 Controller 的 View Controller 的方法。

示例:

// MyViewController.h
@interface MyViewController : UIViewController {
BOOL _shouldPresentSecondModalViewController;
}
@end
// MyViewController.m
@implementation MyViewController
- (void)viewDidAppear:(BOOL)animated {
if(_shouldPresentSecondModalViewController) {
UINavigationController *myNavCon;
// Code to create second modal navigation controller
[self presentModalViewController:myNavCon animated:YES];
_shouldPresentSecondModalViewController = NO;
}
}

- (void)presentFirstViewController {
UINavigationController *myNavCon;
// Code to create the first navigation controller
_shouldPresentSecondModalViewController = YES;
[self presentModalViewController:myNavCon animated:YES];
}
@end

编辑:
现在,如果你想在两个模态视图 Controller 之间传递数据,你可以使用委托(delegate)。
// FirstModalViewControllerDelegate.h
@protocol FirstModalViewControllerDelegate
@optional
- (void)controller:(FirstModalViewControllerDelegate *)vc shouldShowData:(id)anyType;
@end
// MyViewController.h
@interface MyViewController : UIViewController <FirstModalViewControllerDelegate> {
id _dataToDisplay;
}
@end
// MyViewController.m
@implementation MyViewController
- (void)viewDidAppear:(BOOL)animated {
if(_dataToDisplay != nil) {
UINavigationController *myNavCon;
// Code to create second modal navigation controller
[self presentModalViewController:myNavCon animated:YES];
[_dataToDisplay release];
_dataToDisplay = nil;
}
}

- (void)presentFirstViewController {
UINavigationController *myNavCon;
FirstModalViewController *myCon;
// Code to create the first modal view controller

[myCon setDelegate:self];

myNavCon = [[UINavigationController alloc] initWithRootViewController:myCon];

[self presentModalViewController:myNavCon animated:YES];
[myNavCon release];
}

- (void)controller:(FirstModalViewControllerDelegate *)vc shouldShowData:(id)anyType {
/* This method will get called if the first modal view controller wants to display
some data. If the first modal view controller doesn't call this method, the
_dataToDisplay instance variable will stay nil. However, in that case, you'll of
course need to implement other methods to, like a response to a Done button, dismiss
the modal view controller */
[self dismissModalViewController];
_dataToDisplay = [anyType retain];
}
@end

关于ios - 在模态视图 Controller 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8765008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com