为啥我的按钮不起作用?

Posted

技术标签:

【中文标题】为啥我的按钮不起作用?【英文标题】:Why isn't my button working?为什么我的按钮不起作用? 【发布时间】:2013-07-10 05:34:52 【问题描述】:

我创建了一个 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 方法没有任何作用。它似乎根本没有调用它。

感谢任何帮助。

【问题讨论】:

您如何确定该操作方法不会触发?您是否在lldb 中设置了断点或设置了NSLog() 调用? 我只是在第一种情况下放置了一个 NSLog(),它没有调用该函数,因为我没有看到控制台输出。 你的意思是你把它放在switch() 声明之上,不是吗。 【参考方案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;

【讨论】:

哇!谢谢!我还没有真正尝试过使用许多不同类型的参数,所以这是一种新的学习体验,但感谢您帮助我解决了这个看似简单的错误! @user2277872 不用担心。编码愉快。

以上是关于为啥我的按钮不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的轮播顶部的按钮不起作用?

为啥我的后退按钮在 iOS 7 上不起作用?

为啥我的自定义按钮状态不起作用?

为啥我的列表视图上的删除按钮不起作用[重复]

为啥我的停止按钮在这个 javascript 音频播放器上不起作用?

无法弄清楚为啥我的注销按钮不起作用(php)[重复]