有没有更好的方法在Xcode for iPhone应用程序中执行以下操作?
我有一个主导航UIViewController屏幕,上面有4个按钮。
单击按钮1会显示一个子导航UIViewController,上面有4个按钮。 单击按钮2显示相同的子导航UIViewController,但上面有5个按钮。 单击按钮3显示相同的子导航UIViewController,但上面有4个按钮。 单击按钮4显示相同的子导航UIViewController,但上面有6个按钮。
为了解决这个问题,我为主导航中的每个按钮分配了标签。 单击一个按钮时,我会获取此标记号并将其传递给子导航UIViewController。
然后在子导航UIViewController中,根据这个值,我根据需要手动绘制/创建子导航按钮。
以下是我在子导航UIViewController中处理这个问题的方法。 我检查从主导航UIViewController传递的值,并相应地绘制按钮数。我还为每个按钮设置了自定义背景图像。对于每个按钮单击我有自己的选择器方法。请注意,子导航中的某些按钮将转到UIViewController,而某些按钮将转到TableViewController。此外,每个子导航按钮都必须在其目标视图中显示自己的“内容”。
有更好,更优雅的方式处理这个吗?它似乎与我的代码重复很多。下面的例子简洁起见。
// SubNavViewController.m
// SegueTest
#import "SubNavViewController.h"
#import "GettingHereViewController.h"
#import "TableViewController.h"
#import "GettingHereContent.h"
@interface SubNavViewController ()
@end
@implementation SubNavViewController
@synthesize tagNumber;
@synthesize buttonNumber;
@synthesize buttonPushedTrackNumber;
@synthesize hotelButton;
@synthesize bAndBButton;
@synthesize caravanButton;
@synthesize selfCateringButton;
@synthesize apartmentsButton;
.
.
.
etc (19 in total)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.tagNumber == 1)
{
self.navigationItem.title = @"Getting Here";
// By Air Button
byAirButton = [UIButton buttonWithType:UIButtonTypeCustom];
byAirButton.tag = 1;
byAirButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *airButton = [UIImage imageNamed:@"gettingHereByAirButton.png"];
[byAirButton setBackgroundImage:airButton forState:UIControlStateNormal];
[self.view addSubview:byAirButton];
[byAirButton addTarget:self action:@selector(byAirButtonClicked) forControlEvents:UIControlEventTouchUpInside];
// By Rail Button
byRailButton = [UIButton buttonWithType:UIButtonTypeCustom];
byRailButton.tag = 2;
byRailButton.frame = CGRectMake(25, 190, 280.f, 40.f);
UIImage *railButton = [UIImage imageNamed:@"gettingHereByRailButton.png"];
[byRailButton setBackgroundImage:railButton forState:UIControlStateNormal];
[self.view addSubview:byRailButton];
[byRailButton addTarget:self action:@selector(byRailButtonClicked) forControlEvents:UIControlEventTouchUpInside];
.
.
. etc (2 more button)
}
else if (self.tagNumber == 2)
{
self.navigationItem.title = @"Where to Stay";
// B&B Button
bAndBButton = [UIButton buttonWithType:UIButtonTypeCustom];
bAndBButton.tag = 1;
bAndBButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *bedAndBreakfast = [UIImage imageNamed:@"whereToStayBedAndBreakfastButton.png"];
[bAndBButton setBackgroundImage:bedAndBreakfast forState:UIControlStateNormal];
[self.view addSubview:bAndBButton];
[bAndBButton addTarget:self action:@selector(bAndBButtonClicked) forControlEvents:UIControlEventTouchUpInside];
.
.
. etc (do this for the rest of the buttons)
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void) byAirButtonClicked
{
GettingHereContent *gettingHere = [GettingHereContent new];
gettingHere = @"By Air";
NSLog(@"Content: %@", gettingHere);
[self performSegueWithIdentifier:@"gettingHereSegue" sender:self];
}
- (void) bAndBButtonClicked
{
GettingHereContent *gettingHere = [GettingHereContent new];
gettingHere = @"Bed and Breakfast";
NSLog(@"Content: %@", gettingHere);
[self performSegueWithIdentifier:@"tableViewSegue" sender:self];
}
.
.
. (do this for all buttons - 19 in total)
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
@end
答案 0 :(得分:1)
首先,我建议按以下方式创建一个定义所有按钮的NS_ENUM:
typedef NS_ENUM(NSUInteger, ButtonTag)
{
ButtonTagHotel,
ButtonTagOther,
...
}
然后在viewDidLoad中:您可以创建一个包含所有按钮的数组,这些按钮是您当前状态所需的,如下所示:
NSMutableArray *buttonTags = [[NSMutableArray alloc] init];
if (self.tagNumber == 1 || self.tagNumber == 3)
[buttonTags addObject: @(ButtonTagHotel)];
if (self.tagNumber == 5 || self.tagNumber == 8)
[buttonTags addObject: @(ButtonTagOther)];
// etc...
当您构建所需的按钮标签时,您可以在一个循环中创建并添加它们:
uint cnt = 0;
for (NSNumber *tag in buttonTags)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = [tag unsignedIntValue];
btn.frame = CGRectMake(25, 20 + (cnt * 50.f), 280.f, 40.f);
UIImage *image = [UIImage imageNamed:@"genericButtonImage.png"];
[btn setBackgroundImage:image forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
cnt++;
}
如果你必须为每个按钮设置不同的图像,那么你应该创建另一个数组,其中包含由ButtonTag枚举索引的所有图像名称......
现在你只需要实现 - (void)buttonTouched:(UIButton *)sender:
-(void)buttonTouched:(UIButton*)sender
{
switch (sender.tag) {
case ButtonTagHotel:
{
// do your stuff
}
break;
...
default:
break;
}
}