How View Controllers Present Other View Controllers

本文介绍了如何通过编程方式展示模态视图控制器,并详细解释了展示与取消展示过程中的重要步骤。包括设置所需的模态过渡样式、指定委托对象、以及调用当前视图控制器的present方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When you present a modal view controller, the system creates a relationship between the view controller that did the presenting and the view controller that was presented. Specifically, the view controller that did the presenting updates its presentedViewController property to point to its presented view controller. Similarly, the presented view controller updates its presentingViewController property to point back to the view controller that presented it. Figure 10-1 shows the relationship between the view controller managing the main screen in the Calendar app and the presented view controller used to create new events.


Dismissing a view controller dismisses not only that view controller but also any view controllers it presented.


If you need to present a view controller programmatically, you must do the following:

  1. Create the view controller you want to present.
  2. Set the modalTransitionStyle property of the view controller to the desired value.
  3. Assign a delegate object to the view controller. Typically, the delegate is the presenting view controller. The delegate is used by the presented view controllers to notify the presenting view controller when it is ready to be dismissed. It may also communicate other information back to the delegate.
  4. Call the presentViewController:animated:completion: method of the current view controller, passing in the view controller you want to present.

- (void)add:(id)sender {


   // Create the root view controller for the navigation controller


   // The new view controller configures a Cancel and Done button for the


   // navigation bar.


   RecipeAddViewController *addController = [[RecipeAddViewController alloc]


                       init];


 


   // Configure the RecipeAddViewController. In this case, it reports any


   // changes to a custom delegate object.


   addController.delegate = self;



 // Create the navigation controller and present it.


   UINavigationController *navigationController = [[UINavigationController alloc]


                             initWithRootViewController:addController];


   [self presentViewController:navigationController animated:YES completion: nil];


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值