你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UI11_UITabBar

iOS UI11_UITabBar

編輯:IOS開發綜合
//
//  AppDelegate.m
//  UI11_UITabBar
//
//  Created by dllo on 15/8/12.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import AppDelegate.h
#import FirstViewController.h
#import SecondViewController.h
#import ThirdViewController.h
#import FourViewController.h
#import FiveViewController.h
#import SixViewController.h

@interface AppDelegate ()

@end

@implementation AppDelegate
-(void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    //創建一個視圖控制器對象
    FirstViewController *firstVC=[[FirstViewController alloc] init];
    //創建第一個naVC
    UINavigationController *firstNAVC=[[UINavigationController alloc] initWithRootViewController:firstVC];
    //創建tabbar上的按鈕及其內容
    firstVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:1000] autorelease];
    firstVC.tabBarItem.badgeValue =@+99;

    //創建第二個
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    UINavigationController *secondNAVC=[[UINavigationController alloc] initWithRootViewController:secondVC];
    secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@保護費 image:[UIImage imageNamed:@iconfont-jiaofei.png] selectedImage:[UIImage imageNamed:@iconfont-chongzhi.png]];

    ThirdViewController *thirdVC=[[ThirdViewController alloc] init];
    UINavigationController *thirdNAVC=[[UINavigationController alloc] initWithRootViewController:thirdVC];
    thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@進賬 image:[UIImage imageNamed:@iconfont-jiaofei.png] tag:1000];

    FourViewController *fourVC=[[FourViewController alloc] init];
    UINavigationController *fourNAVC=[[UINavigationController alloc] initWithRootViewController:fourVC];
    fourVC.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1000];

    FiveViewController *fiveVC=[[FiveViewController alloc] init];
    UINavigationController *fiveNAVC=[[UINavigationController alloc] initWithRootViewController:fiveVC];
    fiveVC.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1000];

    SixViewController *sixVC=[[SixViewController alloc] init];
    UINavigationController *sixNAVC=[[UINavigationController alloc] initWithRootViewController:sixVC];
    sixVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1000];

#pragma mark 按鈕創建好,然後創建一個UITabbarController讓所有的按鈕顯示出來
    UITabBarController *tabVC = [[UITabBarController alloc] init];
    //UITabBarController通過一個數組來管理所有要顯示出來的naVC
    tabVC.viewControllers = @[firstNAVC,secondNAVC,thirdNAVC,fourNAVC,fiveNAVC,sixNAVC];
    self.window.rootViewController = tabVC;

    //對tabBar進行外觀設置
    tabVC.tabBar.translucent =NO;
    //背景顏色
    tabVC.tabBar.barTintColor=[UIColor orangeColor];
    //點擊之後圖片顏色
    tabVC.tabBar.tintColor=[UIColor yellowColor];
    //先簽訂協議,設置代理人
    tabVC.delegate=self;
    //選第幾個頁面
//    tabVC.selectedIndex = 1;

    //內存管理
    [tabVC release];
    [firstVC release];
    [firstNAVC release];
    [secondVC release];
    [secondNAVC release];
    [thirdVC release];
    [thirdNAVC release];
    [fourNAVC release];
    [fourVC release];
    [fiveVC release];
    [fiveNAVC release];
    [sixNAVC release];
    [sixVC release];

    return YES;
}
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    viewController.tabBarItem.badgeValue =nil;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
//
//  FirstViewController.m
//  UI11_UITabBar
//
//  Created by dllo on 15/8/12.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import FirstViewController.h

@interface FirstViewController ()
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView *tableView;
@end

@implementation FirstViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@宋江, @盧俊義, @吳用, @公孫勝, @關勝, @林沖, @秦明 ,@呼延灼 , @花容,@柴進, @李應, @朱仝,@魯智深,@武松,nil];
    }return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor yellowColor];
    self.navigationController.navigationBar.translucent=NO;

    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView release];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *reuse=@reuse;
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        [cell addGestureRecognizer:longPress];
        [longPress release];


    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(250, 0, 100, 30);
        [button setTitle:@測試 forState:UIControlStateNormal];
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 10;
    [cell addSubview:button];


    }cell.textLabel.text=self.arr[indexPath.row];
    return cell;

}

-(void)click:(UILongPressGestureRecognizer *)longPress
{
    //通過手勢,找到手勢所添加的cell
    UITableViewCell *cell=(UITableViewCell *)longPress.view;
    //創建一個快捷菜單,單例
    UIMenuController *menu = [UIMenuController sharedMenuController];
    //給這個快捷菜單進行定位
    [menu setTargetRect:cell.frame inView:cell.superview];
    //讓菜單顯示
    [menu setMenuVisible:YES animated:YES];
    //如果想使用自定義的功能
    UIMenuItem *flag=[[UIMenuItem alloc] initWithTitle:@測試 action:@selector(flag)];
    //把這個按鈕放到快捷菜單上
    [menu setMenuItems:@[flag]];
    //按鈕方法必須實現,無論系統還是自定義,如果不實現對應的方法,不會添加到快捷菜單

}
#pragma mark 快捷菜單捆綁了一個方法,這個方法必須實現,如果不事先,快捷菜單沒有辦法顯示
-(BOOL)canBecomeFirstResponder
{
    return YES;
}
//-(void)delete:(id)sender
//{
//    NSLog(@刪除);
//}
//-(void)copy:(id)sender
//{
//    NSLog(@復制);
//}
//-(void)select:(id)sender
//{
//    NSLog(@選擇);
//}
//寫自定義的
-(void)flag
{
    NSLog(@faf);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

這裡寫圖片描述

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved