适用于Ipad和IOS的Ios App Iphone不同的导航栏项目

时间:2012-06-03 11:08:28

标签: ios objective-c

我正在开发适用于iPad和iPhone的应用程序,我已经在导航栏中添加了多个条形项目,但我想添加不同的条形项... 在iPad的导航栏中,我只想在右边添加2个项目,这已经有效了。但我还想在iPhone导航栏的左侧添加2个其他按钮。但是通过我正在尝试的方式,我得到了相同的导航栏,相同的项目,以及如何向导航栏添加不同项目的任何想法。

这是我的代码:

UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] 
  initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
  target:self action: @selector(IpadReload:)];

UIImage *emailIcon = [UIImage imageNamed:@"emailicon.png"];
UIBarButtonItem *emailButton = [[UIBarButtonItem alloc] initWithImage:(emailIcon) style:UIBarButtonItemStylePlain target:self action:@selector(emailButtonTapped:)];

NSArray *rightButtonsArray = [[NSArray alloc] initWithObjects:refreshButton, emailButton, nil];

self.navigationItem.rightBarButtonItems = rightButtonsArray;

UIBarButtonItem *previousButton = [[UIBarButtonItem alloc] initWithTitle:@"Previous" style:UIBarButtonItemStylePlain  target: self action: @selector(PreviousClicked:)];

UIImage *phoneIcon = [UIImage imageNamed:@"phoneicon.png"];
UIBarButtonItem *phoneButton = [[UIBarButtonItem alloc] initWithImage:(phoneIcon) style:UIBarButtonItemStylePlain target:self action:@selector(callPhone:)];

NSArray *leftButtonsArray = [[NSArray alloc] initWithObjects:previousButton, phoneButton, nil];

self.navigationItem.leftBarButtonItems = leftButtonsArray;

3 个答案:

答案 0 :(得分:2)

实际上,您需要使用当前设备的iOS内置枚举而不是isEqualToString:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    // iPad
} else {
    //  iPhone or iPod Touch - Add your two items to the right here
}

答案 1 :(得分:1)

区分iPhone和iPad的简洁方法是:

BOOL iPad = [[UIDevice currentDevice] userInterfaceIdiom] == 
    UIUserInterfaceIdiomPad;
// your setup
UIImage *buttonIcon = [UIImage imageNamed: 
   iPad ? @"padIcon.png" : @"phoneIcon.png"];

答案 2 :(得分:0)

首先区分iPhone和iPad并根据它添加导航栏项目     NSString * deviceType = [UIDevice currentDevice] .model;

if([deviceType isEqualToString:@"iPhone"]){
//Add iPhone navigation items
}else if ([deviceType isEqualToString:@"iPhone"]){
//Add iPad navigation items
}else{

// ipod等的默认项目     }