如何自己写一个容器来实现viewController之间的切换

</pre>视图控制器:ViewController<p></p><p><span style="font-size:18px"></span></p><p><span style="font-size:18px">在iOS程序开发中官方提供了四种切换ViewController的方法:</span></p><p><span style="font-size:18px"></span></p><p><span style="font-size:18px">01   模态视图切换</span></p><p><span style="font-size:18px">02    <span style="font-family:Menlo">UINavigationController</span></span></p><p><span style="font-size:18px"><span style="font-family:Menlo">03 </span><span style="font-family:Menlo">UITabBarController</span></span></p><p><span style="font-size:18px"><span style="font-family:Menlo">04 </span><span style="color:rgb(52,149,175); font-family:Menlo">addChildViewController</span></span></p><p><span style="font-size:18px"><span style="color:rgb(52,149,175); font-family:Menlo">   在子视图控制中如何获取容器视图控制器呢?</span></span></p><p><span style="font-size:18px"><span style="color:rgb(52,149,175); font-family:Menlo"><span style="white-space:pre">	</span></span></span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(0, 143, 0);"><span style="font-family: 'Helvetica Neue';"><span style="white-space:pre">	</span>//</span>找到当前视图控制器所在的容器</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(52, 149, 175);"><span style="color: #000000">    </span>PSBMedicalViewController<span style="color: #000000"> * mvc = (</span><span style="font-family: 'Helvetica Neue'; color: rgb(4, 51, 255);">id</span><span style="color: #000000">)</span><span style="font-family: 'Helvetica Neue'; color: rgb(4, 51, 255);">self</span><span style="color: #000000">.</span>parentViewController<span style="color: #000000">;</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; min-height: 21px;">    </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(52, 149, 175);"><span style="color: #000000">    [mvc </span>popRightDrawer<span style="color: #000000">];</span></p><p><span style="font-size:18px; color:#cc0000"><span style="font-family:Menlo">第四种方式我们用来自定义容器时,切换ViewController。</span></span></p><p><span style="font-size:18px"><span style="color:rgb(52,149,175); font-family:Menlo"></span></span></p><p><span style="font-size:18px"><span style="font-family:Menlo">视图控制器有两类:1:显示的视图控制器 2:不显示的视图控制器,用来管理其他子视图控制器的视图控制器。称为:容器视图控制器。</span></span></p><p></p><p><span style="white-space:pre">	</span><span style="font-size:24px;color:#cc0000;">注意:自定义的容器视图控制器,是不能被释放的。为此,有多种方式让其不被释放,1:单例,2:交给NavigationBar中ViewControllers这个数组。</span></p><p></p><p></p><pre name="code" class="objc">#import "PSBRootViewController.h"

/**自定义的容器控制器,管理其他的视图控制器,本身不显示*/
@interface AZMedicalViewController : PSBRootViewController


//弹出右侧的抽屉
-(void)popRightDrawer;

//收起右侧抽屉
-(void)hideRightDrawer;
@end

#import "AZMedicalViewController.h"
#import "AZFirstLayerModel.h"

@interface AZMedicalViewController ()
{
    //判断当前抽屉是否显示
    BOOL _isDrawerShow;

}

@end

@implementation AZMedicalViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    //创建相关子视图
   [self createSubView];
    
    //在这里,添加被管理的子视图控制器
    [self createChildViewcontrollers];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//创建相关子视图
-(void)createSubView
{
    PSBBarButtonItem *backButton=[PSBViewFactory backButton];
    [backButton addTarget:self action:@selector(backButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_nvgItem addSubview:backButton];
}


-(void)backButtonOnClick:(PSBBarButtonItem *)item
{
    
    if (!_isDrawerShow)
    {
       [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self hideRightDrawer];
    }
}

#pragma mark -- 容器视图控制器的相关方法
//添加子视图控制器
-(void)addChildViewControllers:(NSArray *)vcs
{
    if (vcs.count!=2) {
        return;
    }
    
    for (NSUInteger i=0; i<2; i++)
    {
        PSBRootViewController *vc=vcs[i];
        
        //让出导航条
        CGRect frame=self.view.bounds;
        frame.origin.y += 64;
        frame.size.height -=64;
        if (i==0) {
            vc.view.frame=frame;
        }
        else
        {
            
            frame.size.width=frame.size.width/2;
            frame.origin.x=self.view.bounds.size.width;
            vc.view.frame=frame;
        }
        
        [self.view addSubview:vc.view];
        //但是需要注意:如果在ARC下,self.view 只添加了vc.view的话,那么arc会自动释放掉VC。
        
        //解决办法:
        //官方视图控制器,提供方法保留子视图控制器。
        //使当前视图控制器作为容器,子视图控制器不会被释放。其实内部实现也是很简单,就是一个数组,管理着这些子视图控制器,强引用。
        [self addChildViewController:vc];
        
    }
 
}

//弹出右侧的抽屉
-(void)popRightDrawer
{
    if (_isDrawerShow) {
        return;
    }
    
    [UIView beginAnimations:nil context:NULL];
    
    [UIView setAnimationDuration:0.25];
    
    //设置动画结束的效果
    PSBRootViewController *vc1=self.childViewControllers[0];
    CGRect frame=vc1.view.frame;
    frame.size.width=frame.size.width/2;
    vc1.view.frame=frame;
    
    PSBRootViewController *vc2=self.childViewControllers[1];
    frame=vc2.view.frame;
    frame.origin.x=vc1.view.frame.size.width;
    vc2.view.frame=frame;

    [UIView commitAnimations];
  
    _isDrawerShow=YES;
}

//收起右侧抽屉
-(void)hideRightDrawer
{
    if (!_isDrawerShow) {
        return;
    }
    [UIView beginAnimations:nil context:NULL];
    
    [UIView setAnimationDuration:0.25];
    
    //设置动画的效果
    CGRect frame=self.view.bounds;
    frame.origin.y +=64;
    frame.size.height -=64;
    
    PSBRootViewController *vc1=self.childViewControllers[0];
    vc1.view.frame=frame;
    
    PSBRootViewController *vc2=self.childViewControllers[1];
    frame.origin.x=vc1.view.frame.size.width;
    frame.size.width=self.view.bounds.size.width/2;
    vc2.view.frame=frame;
    
    [UIView commitAnimations];
    
    _isDrawerShow=NO;
    
}


#pragma mark -- 子视图控制器
//创建子视图控制器
-(void)createChildViewcontrollers
{
    NSArray *tempArray=@[@"AZFirstViewController",@"AZSecondViewController"];
    id obj1,obj2;
    
    for (NSUInteger i=0; i<tempArray.count; i++)
    {
        Class cls=NSClassFromString(tempArray[i]);
        id obj=[[cls alloc] init];
        if (i) {
            obj2=obj;
        }
        else
        {
            obj1=obj;
        }
    }
    
    //添加到当前容器
    [self addChildViewControllers:@[obj1,obj2]];
}

@end


//选中了一个cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //找到当前视图控制器所在的容器
    PSBMedicalViewController * mvc = (id)self.parentViewController;
    
    [mvc popRightDrawer];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的ExtJS 6模糊查询的例子: 1. 创建一个包含一个文本框和一个按钮的视图: Ext.define('MyApp.view.Main', { extend: 'Ext.panel.Panel', xtype: 'main', title: 'My Application', width: 400, bodyPadding: 10, items: [{ xtype: 'textfield', fieldLabel: 'Name', name: 'name', allowBlank: false }, { xtype: 'button', text: 'Search', handler: 'onSearchClick' }] }); 2. 创建一个控制器,在按钮点击时执行模糊查询: Ext.define('MyApp.view.MainController', { extend: 'Ext.app.ViewController', alias: 'controller.main', onSearchClick: function() { var name = this.lookupReference('name').getValue(); var store = this.getViewModel().getStore('users'); store.clearFilter(); store.filter('name', name); } }); 3. 创建一个模型和一个存储,用于保存用户数据: Ext.define('MyApp.model.User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email'] }); Ext.define('MyApp.store.Users', { extend: 'Ext.data.Store', alias: 'store.users', model: 'MyApp.model.User', data: [{ id: 1, name: 'John Smith', email: 'john.smith@example.com' }, { id: 2, name: 'Jane Doe', email: 'jane.doe@example.com' }, { id: 3, name: 'Bob Johnson', email: 'bob.johnson@example.com' }] }); 4. 创建一个视图模型,将存储添加到视图模型中: Ext.define('MyApp.view.MainModel', { extend: 'Ext.app.ViewModel', alias: 'viewmodel.main', stores: { users: { type: 'users' } } }); 5. 将视图、控制器和视图模型组合在一起: Ext.application({ name: 'MyApp', launch: function() { Ext.create('MyApp.view.Main', { renderTo: Ext.getBody() }); } }); 这个例子中的搜索功能将从“users”存储中筛选出名字包含文本框中输入的值的用户。这个例子可以轻松地扩展以实现更复杂的模糊查询。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值