UITabBarItem 快捷菜单

AppDelegate.m


// 创建六个tabBarItem

#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "FifthViewController.h"
#import "sixthViewController.h"

@interface AppDelegate ()<UITabBarDelegate,UITabBarControllerDelegate>

@end

@implementation AppDelegate
#define SELECTEDKEY @"selected"
-(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 *firNAVC=[[UINavigationController alloc]initWithRootViewController:firstVC];

    // 创建tabbar上的按钮及其内容
    // 自定义方法创建Item
    firstVC.tabBarItem=[[[UITabBarItem alloc]initWithTitle:@"music" image:[UIImage imageNamed:@"iconfont-music.png"] tag:2]autorelease];
    firstVC.tabBarItem.badgeValue=@"+99";

    // 用系统的方法创建Item
//    firstVC.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];


    SecondViewController *secondVC=[[SecondViewController alloc]init];
    UINavigationController *secNAVC=[[UINavigationController alloc]initWithRootViewController:secondVC];
//    secondVC.tabBarItem=[[[UITabBarItem alloc]initWithTitle:@"lalala" image:[UIImage imageNamed:@"iconfont-meishi.png"] selectedImage:[UIImage imageNamed:@"iconfont-gaoxiaoquwei.png"]]autorelease];
    secNAVC.tabBarItem=[[[UITabBarItem alloc]initWithTitle:@"lalala" image:[UIImage imageNamed:@"iconfont-meishi.png"] selectedImage:[UIImage imageNamed:@"iconfont-gaoxiaoquwei.png"]]autorelease];


    ThirdViewController *thirdVC=[[ThirdViewController alloc]init];
    UINavigationController *thiNAVC=[[UINavigationController alloc]initWithRootViewController:thirdVC];
    thirdVC.tabBarItem=[[[UITabBarItem alloc]initWithTitle:@"baobao" image:[UIImage imageNamed:@"iconfont-baobao.png"] tag:2]autorelease];


    FourthViewController *fourVC=[[FourthViewController alloc]init];
    UINavigationController *fourNAVC=[[UINavigationController alloc]initWithRootViewController:fourVC];
    fourVC.tabBarItem=[[[UITabBarItem alloc]initWithTitle:@"qinggan" image:[UIImage imageNamed:@"iconfont-qinggan.png"] tag:4]autorelease];


    FifthViewController *fiveVC=[[FifthViewController alloc]init];
    UINavigationController *fiveNAVC=[[UINavigationController alloc]initWithRootViewController:fiveVC];
    fiveVC.tabBarItem=[[[UITabBarItem alloc]initWithTitle:@"dress" image:[UIImage imageNamed:@"iconfont-nvzhuang.png"] tag:5]autorelease];


    sixthViewController *sixVC=[[sixthViewController alloc]init];
    UINavigationController *sixNAVC=[[UINavigationController alloc]initWithRootViewController:sixVC];
    sixVC.tabBarItem=[[[UITabBarItem alloc]initWithTitle:@"pets" image:[UIImage imageNamed:@"iconfont-chongwu.png"] tag:6]autorelease];



#pragma mark 按钮创建好,然后创建一个UITabBarController让所有的按钮显示出来

    UITabBarController *tabVC=[[UITabBarController alloc]init];
//    tabVC.tabBar.backgroundImage
    // tabBarController通过一个数组来管理所有要显示出来的naVC;
    tabVC.viewControllers=@[firNAVC,secNAVC,thiNAVC,fourNAVC,fiveNAVC,sixNAVC];
    self.window.rootViewController=tabVC;


    // 保存前一次退出工程时的状态
    NSString *viewControllerTitle=[[NSUserDefaults standardUserDefaults]objectForKey:SELECTEDKEY];
    if (viewControllerTitle) {
        for (UIViewController *vc in tabVC.viewControllers) {
            if ([vc.tabBarItem.title isEqualToString:viewControllerTitle]) {
                tabVC.selectedViewController=vc;
            }
        }
    }

    // tabBarController的代理
    tabVC.delegate=self;

    // tabBar进行外观设置
    // 背景图片
    tabVC.tabBar.backgroundImage=[UIImage imageNamed:@"6.jpg"];

    // 背景  字体  颜色



    // 运行后直接选择第三个开始
//    tabVC.selectedIndex=2;


    // 释放
    [tabVC release];
    [firstVC release];
    [firNAVC release];
    [secondVC release];
    [secNAVC release];
    [thirdVC release];
    [thiNAVC release];
    [fourVC release];
    [fourNAVC release];
    [fiveVC release];
    [firNAVC release];
    [sixVC release];
    [sixNAVC release];


    return YES;
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    // 去掉item上的+99
    viewController.tabBarItem.badgeValue=nil;
    // 指向工程中的plist文件
    NSUserDefaults *defatults=[NSUserDefaults standardUserDefaults];
    if (viewController.tabBarItem.title) {
        [defatults setObject:viewController.tabBarItem.title forKey:SELECTEDKEY];
        // 将数据同步给应用程序
        [defatults synchronize];
    }

}

快捷菜单

FirstViewController.m

#import "FirstViewController.h"

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

@implementation FirstViewController
-(void)dealloc
{
    [_tableView release];
    [_arr release];
    [super dealloc];
}
-(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 redColor];

    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:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    [self.tableView release];


}

-(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:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];

        UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
        [cell addGestureRecognizer:longPress];
        [longPress release];


//        UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
//        [button setTitle:@"ceshi" forState:UIControlStateNormal];
//        [cell addSubview:button];
//        button.frame=CGRectMake(300, 0, 100, 40);
//        

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

// longPress 的方法
-(void)click:(UILongPressGestureRecognizer *)longPress
{
    NSLog(@"fdlsjdlsa");
    // 通过手势,找到手势所添加的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)];
    UIMenuItem *jfkd=[[UIMenuItem alloc]initWithTitle:@"开心" action:@selector(jfkd)];
    // 把这个按钮放到快捷菜单上
    [menu setMenuItems:@[jfkd,flag,jfkd]];

    // 按钮的方法必须要实现,无论系统还是自定义,如果不实现对应的方法,不会添加到快捷菜单上


}

#pragma mark 快捷菜单捆绑了一个方法,这个方法必须实现,如果不实现,快捷菜单没有办法显示
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)flag
{
    NSLog(@"测试");
}

-(void)jfkd
{
    NSLog(@"开心");
}
-(void)delete:(id)sender
{
    NSLog(@"删除");
}

-(void)copy:(id)sender
{
    NSLog(@"复制");
}

-(void)select:(id)sender
{
    NSLog(@"选择");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值