我有一个理论问题.现在我正在阅读Apple的ViewController指南.
他们写:
当需要关闭呈现的视图控制器时,首选方法是让呈现视图控制器关闭它.换句话说,只要有可能,呈现视图控制器的同一视图控制器也应负责解除它.尽管有几种技术用于通知呈现视图控制器应该解除其呈现的视图控制器,但是优选的技术是委托.
但我无法解释,为什么我必须在呈现的VC中创建一个协议并添加委托变量,在呈现VC中创建委托方法以解除所呈现的VC,而不是在呈现的视图控制器方法中的简单调用
[self dismissViewControllerAnimated:NO completion:nil]?
为什么第一选择更好?为什么Apple推荐它?
当按下按钮时,我想通过使用模态过渡样式在两个视图控制器之间进行切换CoverVertical,然后将其关闭.有关于如何在目标C中执行此操作的信息,但在Swift中找不到任何好的信息.到目前为止我已经做到了这一点,但我不认为这是正确的:
@IBAction func insertStatus(sender: UIButton) {
var StatusVC: StatusViewController = StatusViewController()
var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
StatusVC.modalTransitionStyle = modalStyle
self.presentViewController(StatusVC, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
我这样使用的解雇也不起作用:
@IBAction func statusSaved(sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(false, completion: { () -> Void in
let usersVC: UsersViewController = self.storyboard?.instantiateViewControllerWithIdentifier("UsersViewController") as UsersViewController
})
}
Run Code Online (Sandbox Code Playgroud) 我正在以下列方式创建警报:
let alert = UIAlertView(title: "Network Unavailable",
message: "Oh noes!",
delegate: nil,
cancelButtonTitle: "OK")
alert.show()
Run Code Online (Sandbox Code Playgroud)
工作良好.但是当我点击"确定"按钮关闭警报时,我得到了这个:
警告:正在进行演示或解除时,尝试从视图控制器<_UIAlertShimPresentingViewController:0x16ea2230>中解除!
一些背景:
有什么想法可能会发生这种警告?如果它在未来的iOS版本中变成致命错误,我不想忽略它.
UPDATE
我还应该补充一点,当警报出现时,当我选择Debug - > View Debugging - > Capture View Hierarchy时,警报不会显示在视图的3d视图中.我想知道这是否是我做错了的症状.
我已经查看过相关问题,但没有解决我的问题.
我试图使用dismissViewControllerAnimated:animated:completion和presentViewControllerAnimated:animated:completion连续.使用故事板,我通过部分卷曲动画模拟呈现InfoController.
部分卷曲显示我想要启动的InfoController上的按钮MFMailComposeViewController.因为部分卷曲部分隐藏了MFMailComposeViewController,我首先要通过取消动画部分卷曲来解除InfoController.然后我想要MFMailComposeViewController动画片.
目前,当我尝试这个时,部分卷曲没有动画,但是MFMailComposeViewController没有得到呈现.我也有一个警告:
警告:尝试在InfoController上显示MFMailComposeViewController:其视图不在窗口层次结构中!
InfoController.h:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface InfoController : UIViewController <MFMailComposeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *emailMeButton;
-(IBAction)emailMe:(id)sender;
@end
Run Code Online (Sandbox Code Playgroud)
InfoController.m
#import "InfoController.h"
@interface InfoController ()
@end
@implementation InfoController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)emailMe:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
[self sendMeMail];
}];
}
- (void)sendMeMail {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
if([MFMailComposeViewController canSendMail]){
if(mailController)
{
NSLog(@"%@", self); // This returns …Run Code Online (Sandbox Code Playgroud) 我有一个名为sendDataToMotor的函数.它在我的First View Controller类中.我有另一个名为SecondViewController的视图控制器.我需要从Second View Controller.m类调用此函数.我试着宣布这个属性:
@property(nonatomic,assign)UIViewController* firstController;
Run Code Online (Sandbox Code Playgroud)
在我的SecondViewController.h类中.此外,我在我的SecondViewController.m类的viewDidLoad部分编写了代码(我希望调用该函数).
secondViewController = [[SecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
secondViewController.firstController = self;
[self.firstController performSelector:@selector(sendDataToMotor)];
Run Code Online (Sandbox Code Playgroud)
但是,由于未声明的标识符问题,我在该代码(secondViewController)中的第一个单词出错.此外,我在第二行(secondViewController.firstController = self)中出错,因为secondViewController具有未知的名称类型.
总而言之,我不在乎你是否使用上面的代码来回答我的问题:这只是我试图在网上找到的东西.但是,我正在寻找从另一个View Controller调用函数的最简单方法.
ios ×4
objective-c ×3
iphone ×2
cocoa-touch ×1
dismiss ×1
segue ×1
skscene ×1
sprite-kit ×1
swift ×1
uialertview ×1
warnings ×1