我创建了一个UIButton;实际上,有几个按钮,我已经初始化它们并在
中设置它们-(void)viewDidLoad
方法。我现在的问题是将它们连接到我创建的方法。我已经创建了一个typedef枚举来替换我的按钮的标签的数字与某些情况下的名称。我不知道这是问题还是什么,但我很困惑为什么当我按下按钮时,它们不起作用。我的代码如下。
我的枚举:
typedef enum {
DiseasesStrepThroat = 1,
DiseasesFlu = 2,
DiseasesChickenPox = 3,
DiseasesSmallPox = 4,
DiseasesMeasels = 5,
DiseasesCommonCold = 6
} DiseasesTagNumber;
我初始化了所有UIButton对象;以及我在我的.h文件中的IBAction,所以我不必担心那部分。
这是我的观点加载
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 700, 768, 125)];
scrollView.contentSize = CGSizeMake(1000, 125);
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.autoresizesSubviews = NO;
[self.view addSubview:scrollView];
//////////////////////////////////////////
// //
// creating all the buttons //
// //
//////////////////////////////////////////
// strep throat
strep = [UIButton buttonWithType:UIButtonTypeRoundedRect];
strep.frame = CGRectMake(10, 10, 110, 100);
[strep setTitle:@"Strep" forState:UIControlStateNormal];
[strep setTag:DiseasesStrepThroat];
[scrollView addSubview:strep];
// the flu
flu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
flu.frame = CGRectMake(130, 10, 110, 100);
[flu setTitle:@"Flu" forState:UIControlStateNormal];
[flu setTag:DiseasesFlu];
[scrollView addSubview:flu];
// chicken pox
chickenPox = [UIButton buttonWithType:UIButtonTypeRoundedRect];
chickenPox.frame = CGRectMake(250, 10, 110, 100);
[chickenPox setTitle:@"Chicken Pox" forState:UIControlStateNormal];
[chickenPox setTag:DiseasesChickenPox];
[scrollView addSubview:chickenPox];
// small pox
smallPox = [UIButton buttonWithType:UIButtonTypeRoundedRect];
smallPox.frame = CGRectMake(370, 10, 110, 100);
[smallPox setTitle:@"Small Pox" forState:UIControlStateNormal];
[smallPox setTag:DiseasesSmallPox];
[scrollView addSubview:smallPox];
// measels
measels = [UIButton buttonWithType:UIButtonTypeRoundedRect];
measels.frame = CGRectMake(490, 10, 110, 100);
[measels setTitle:@"Measels" forState:UIControlStateNormal];
[measels setTag:DiseasesMeasels];
[scrollView addSubview:measels];
// common cold
commonCold = [UIButton buttonWithType:UIButtonTypeRoundedRect];
commonCold.frame = CGRectMake(610, 10, 110, 100);
[commonCold setTitle:@"Common Cold" forState:UIControlStateNormal];
[commonCold setTag:DiseasesCommonCold];
[scrollView addSubview:commonCold];
NSArray *array = [[NSArray alloc] initWithObjects:strep, flu, chickenPox, smallPox, measels, commonCold, nil];
for(UIButton *b in array) {
b.showsTouchWhenHighlighted = YES;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
// //
// CREATING ALL THE TARGETS //
// //
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
[strep addTarget:self action:@selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[flu addTarget:self action:@selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[chickenPox addTarget:self action:@selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[smallPox addTarget:self action:@selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[measels addTarget:self action:@selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[commonCold addTarget:self action:@selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
}
现在我的IBAction方法
- (IBAction) showDiseasesWithTag:(NSInteger)senderTag {
switch (senderTag) {
case DiseasesStrepThroat:
a = [[UIAlertView alloc]
initWithTitle:@"H"
message:@"yo"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[a show];
break;
case DiseasesFlu:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesChickenPox:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesSmallPox:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesMeasels:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesCommonCold:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
}
}
要清除任何内容,所有UIView动画和完成块都没有完成。我主要关注第一个案例。是因为我的第一个案例是1吗?当我将它设置为0时,它仍然没有做任何事情。
一切的实际问题是,当我在我的UIScrollView中按下第一个UIButton时,我能够按下它,但是发生的事情是该按钮对IBAction方法没有任何作用。它似乎根本没有调用它。
感谢任何帮助。
答案 0 :(得分:1)
好的,你只是对action方法使用了错误的参数。它应该是:
- (IBAction) showDiseasesWithTag:(id)sender {
NSInteger senderTag = [sender tag];
switch (senderTag) {
...
}
}
在iOS下,目标方法可以有三种不同的签名之一:
- (IBAction)actionMethod;
- (IBAction)actionMethod:(id)sender;
- (IBAction)actionMethod:(id)sender forEvent:(UIEvent *)event;
但从不:
- (IBAction)actionMethod:(NSInteger)senderTag;