2013-07-10 114 views
0

我创建了一个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动画和完成块未完成。我主要关注第一个案例。是因为我有第一个案例吗?当我将它设置为0时,它仍然没有任何作用。

所有事情的实际问题是,当我按下我的UIScrollView中的第一个UIButton时,我能够按下它,但是会发生什么情况是按钮对IBAction方法没有做任何事情。它似乎根本没有调用它。

任何帮助表示赞赏。

+0

你怎么确定的操作方法不不开火?你在'lldb'中设置了一个断点还是设置了一个'NSLog()'调用? – trojanfoe

+0

我刚刚在第一个案例中放置了一个NSLog(),并且它没有调用该函数,因为我看不到任何控制台输出。 – user2277872

+0

你的意思是你把它放在'switch()'语句之上,不是吗。 – trojanfoe

回答

1

好的,你只是对动作方法使用了错误的参数。它应该是:

- (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; 
+0

哇!谢谢!我没有真正尝试过使用许多不同类型的论据,所以这是一种全新的学习体验,但是感谢您帮助我解决看似简单的错误! – user2277872

+0

@ user2277872不用担心。快乐的编码。 – trojanfoe

相关问题