iOS开发之-Custom View Controller 2

Creating Custom View Controller Objects at Runtime


MyViewController* vc = [[MyViewController alloc] initWithNibName:@"MyViewController"
                             bundle:nil];


1. A common approach to take when designing custom view controllers though is to define one or more custom initialization methods. Doing so allows you to hide some of the more immutable aspects of the view controller initialization (such as specifying the nib file and bundle names) and focus instead on the data you want to use to initialize the view controller. 


- (id)initWithData:(NSArray*)data {
    if ((self = [super initWithNibName:@"MyViewController" bundle:nil])) {
        // Initialize the view controller with the starting data
    }
    return self;
}

2. When creating view controllers programmatically, it is your responsibility to set the frame of the view controller’s view appropriately before using the view. A programmatically created view controller that loads its view from a nib file does not try to change the size or position of that view. If the view controller is presented modally or used with a container view controller, the parent or container view controller often adjusts the view for you. But in cases where you add the view to a window yourself, the view’s existing frame is used as is. If your application has a status bar, failing to adjust the view’s frame could cause the misplacement of the view underneath the status bar.


Presenting a View Controller’s View


1. There are several options for displaying the view associated with a view controller:

  • Display the view directly by adding it to a window using theaddSubview: method.
  • Display the view indirectly using one of the following techniques:
    • Present the owning view controller modally using thepresentModalViewController:animated: method.
    • Push the owning view controller onto the navigation stack of a navigation controller object.
    • Make the owning view controller the root view controller of a tab in a tab bar interface.
    • On iPad, present the view controller using a popover.

- (void)applicationDidFinishLaunching:(UIApplication *)application {
   // At this point, the main nib file is loaded.
   // It is a good idea to set the view's frame before adding it to a window.
   [viewController.view setFrame:[[UIScreen mainScreen] applicationFrame]];
 
   [window addSubview:viewController.view];
   [window makeKeyAndVisible];
}

2. If you plan to add a view to a window using the addSubview: method, it is recommended that you explicitly set the frame of the view after loading it from a nib file.  If you plan to present a view modally or use it in conjunction with a container view controller, you do not need to set the frame explicitly.


Responding to Display-Related Notifications






Adopting a Full-Screen Layout for Custom Views


1. If your application displays a translucent status bar, you can set the value of your view controller’s wantsFullScreenLayout property to YES to allow your view to underlap the status bar.


Enabling Edit Mode for a View


1. If you want to use the same view controller to display and edit content, you can override the setEditing:animated: method and use it to toggle the view controller’s view between display and edit modes. 


- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
    [super setEditing:flag animated:animated];
    if (flag == YES){
        // change views to edit mode
    }
    else {
        // save the changes if needed and change views to noneditable
    }
}



Handling Events





Accessing Related View Controller Objects



 

A custom view controller is responsible for providing any specific objects needed by higher-level view controllers. 

If you do not specify a custom object for any of these properties, the view controller provides an appropriate default item for you.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值