ios学习笔记(5)UITabBarController用法

来源:互联网 发布:公司数据安全管理 编辑:程序博客网 时间:2024/06/07 03:34

 转载请注明出处: http://blog.csdn.net/wudiwo/article/details/7765788

       ihone中的TabBar对应的Android 中的TabHost ,安卓中的TabHost用法有两种,一种是继承TabActivity类Intent方式进行跳转,然后代码中使用

具体实现方式如下: 

public static final String TAB_HOME="首页";
public static final String TAB_MSG="信息"; 

 TabHost mytabhost=getTabHost(); 

  TabSpec ts1=mytabhost.newTabSpec(TAB_HOME).setIndicator(TAB_HOME);
        ts1.setContent(new Intent(MainActivity.this,AboutActivity.class));
        mytabhost.addTab(ts1);

 TabSpec ts2=mth.newTabSpec(TAB_MSG).setIndicator(TAB_MSG);
        ts2.setContent(new Intent(MainActivity.this,SecondActivity.class));
        mth.addTab(ts2);

另一种通过找.xml文件中配置的ID的方式  如下: 

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView android:id="@+id/view1"
        android:background="@drawable/blue"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/tabs_1_tab_1"/>


    <TextView android:id="@+id/view2"
        android:background="@drawable/red"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/tabs_1_tab_2"/>


    <TextView android:id="@+id/view3"
        android:background="@drawable/green"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/tabs_1_tab_3"/>


</FrameLayout>

程序中具体实现代码: 

 setContentView(R.layout.main);   
        TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);

  接下来很重要的一步是要使用

     TabHost.setup();
  作用是来初始化我们的TabHost容器:

     LayoutInflater inflater_tab1 = LayoutInflater.from(this);   
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView());  
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.view1));   
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB b").setContent(R.id.view2)); 

综上:要实现TabHost的效果离不开这三个对象:TabHost 、TabWidget  、TabSpec 

        其实在Android中还有一种比较经典的实现TabHost的效果的方式: TabHost + RadioGroup的方式,这里不再细说,需要说明一点这里的TabWidget实际上是gone的,用RadioGroup来代替具体的显示,好了文章已经远远偏离标题了,下面把话题收回来说一下iphone中 UITabBarController的用法。这里我说到现在为止我知道的两种法:

          一、在MainWindow.xib 文件中直接添加UITabBarController 这的用法实际上和UINativationController的用法基本相同。当然你也可以在Delegate中用代码方式来实现这个UITabBarController,相对来说比较简单,这里重点说一下第二中效果的实现方式。

          二、有时候我们做东西的时候需要开始第一个页面是用户登录页面,填好用户登录信息成功登录后才会出现带有UITabBarController的页面,很显然如果还用第一种方式来做的话我们是很难实现这个流程的,那么我们怎样才能在第二个页面才开始出现UITabBarController呢?这里总结了一下

具体应用大概有下面几个步骤: 

  1、定义自己的MyUITabController类继承 UITabBarController 并且要实现 UITabBarDelegate 这个协议。

这里要实现父类的方法: 

  -(void) tabBar:(UITabBar *) tabBar didSelectItem:(UITabBarItem *)item{

               if(item.tag==1){

                   // 点击TabBar的第一个选项要显示东西  OneActivity.m 页面

                     }else if(item.tag==2){

                    // 点击TabBar第二个选项要显示的东西 TwoActivity.m的页面

                   } else{

                    // 其他

        }

}

2、在第一个页面实现点击事件

   NSMutableArray *item=[[[NSMutableArray alloc] init] autorelease];

  OneActivity *oneactivity=[[[OneActivity alloc]init] autorelease];

   [item addObject:oneActivity];

  TwoActivity *twoactivity=[[[TwoActivity alloc]init] autorelease];

   [item addObject:twoactivity];

MyUITabController *myController=[[[MyUITabController alloc] init]autorelease];

 [myController setViewControllers:item];//将数据中的元素交个myController来管理

[self presentModelViewController : myControler animated:YES];// 实现跳转,这里是presentModelViewController 的方式.

3、设置相应的TabBar的显示图片和文字可以在相应的OneActivity.m 的

  -(id) initWithNibName:(NSString *) nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    if((self=[self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])){

 UITableBarItem *item=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemHistory tag:2];// 这里设置的tag是和前面切换 的时候具体显示谁的内容是相关联的,这里用的是系统自己的图片和文字样式,当然还可以自己定义想要的方式

self.tabBarItem=item;

  }

return self;

  }

  到这里想要的效果基本上就已经完全实现了。由于相应的代码都在公司的苹果机上,抽时间再把代码上传了,希望写了这些能对你有所帮助。

转载请注明出处: http://blog.csdn.net/wudiwo/article/details/7765788