【发布时间】:2013-12-17 02:25:35
【问题描述】:
我有一个 UIButton,它有多种方法。正如您将在我的代码中看到的那样,每次按下 UIButton 时,它都会更改其标签,从而让我让它每次都做不同的事情(所以第一次做事情 A,第二次做事情 B,等等)。我确实发现由于某种原因 iPhone 一次会执行两种这样的方法,所以我在每个真实的方法之间添加了一个无用的方法。这工作得很好,但由于某种原因它刚刚停止工作,我不知道为什么。现在它在第一次按下 UIButton 时就出现了线程中断。任何帮助表示赞赏。
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextViewDelegate> {
IBOutlet UITextView *textEnterView;
IBOutlet UILabel *promptLabel;
IBOutlet UIButton *nextButton;
NSString *topicString;
NSString *firstString;
NSString *firstSubString;
NSString *secondString;
NSString *secondSubString;
NSString *thirdString;
NSString *thirdSubString;
NSString *conclusionString;
}
@property (nonatomic, retain) UITextView *textEnterView;
@property (nonatomic, retain) UILabel *promptLabel;
@property (nonatomic, retain) UIButton *nextButton;
@property (nonatomic, retain) NSString *topicString;
@property (nonatomic, retain) NSString *firstString;
@property (nonatomic, retain) NSString *firstSubString;
@property (nonatomic, retain) NSString *secondString;
@property (nonatomic, retain) NSString *secondSubString;
@property (nonatomic, retain) NSString *thirdString;
@property (nonatomic, retain) NSString *thirdSubString;
@property (nonatomic, retain) NSString *conclusionString;
- (IBAction) next:(id)sender;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize textEnterView, promptLabel, nextButton, firstSubString, firstString, secondSubString, secondString, thirdSubString, thirdString, conclusionString, topicString;
- (void)viewDidLoad
{
UIButton *button = nextButton;
[button setTag:1234];
[button addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
textEnterView.delegate = self;
promptLabel.text = @"First Label";
textEnterView.text = @" ";
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction) next:(UIButton *)sender {
if(nextButton.tag==1234) {
promptLabel.text = @"Second Label";
[nextButton setTag:2];
topicString = textEnterView.text;
//FAKE METHOD
}
else if (nextButton.tag==2) {
promptLabel.text = @"Third Label";
textEnterView.text = @" ";
[nextButton setTag:3];
//REAL METHOD
}
else if (nextButton.tag==3) {
promptLabel.text = @"Fourth Label";
firstString = textEnterView.text;
[nextButton setTag:4];
//FAKE METHOD
}
else if (nextButton.tag==4) {
promptLabel.text = @"Fifth Label";
textEnterView.text = @" ";
[nextButton setTag:5];
//REAL METHOD
}
else if (nextButton.tag==5) {
promptLabel.text = @"Sixth Label";
firstSubString = textEnterView.text;
[nextButton setTag:6];
//FAKE METHOD
}
else if (nextButton.tag==6) {
promptLabel.text = @"Seventh Label";
textEnterView.text = @" ";
[nextButton setTag:7];
//REAL METHOD
}
else if (nextButton.tag==7) {
promptLabel.text = @"Eighth Label";
secondString = textEnterView.text;
[nextButton setTag:8];
//FAKE METHOD
}
else if (nextButton.tag==8) {
promptLabel.text = @"Ninth Label";
textEnterView.text = @" ";
[nextButton setTag:9];
//REAL METHOD
}
else if (nextButton.tag==9) {
promptLabel.text = @"Tenth Label";
secondSubString = textEnterView.text;
[nextButton setTag:10];
//FAKE METHOD
}
else if (nextButton.tag==10) {
promptLabel.text = @"Eleventh Label";
textEnterView.text = @" ";
[nextButton setTag:11];
//REAL METHOD
}
else if (nextButton.tag==11) {
promptLabel.text = @"Twelfth Label";
thirdString = textEnterView.text;
[nextButton setTag:12];
//FAKE METHOD
}
else if (nextButton.tag==12) {
promptLabel.text = @"Thirteenth Label";
textEnterView.text = @" ";
[nextButton setTag:13];
//REAL METHOD
}
else if (nextButton.tag==13) {
promptLabel.text = @"Fourteenth Label";
thirdSubString = textEnterView.text;
[nextButton setTag:14];
//FAKE METHOD
}
else if (nextButton.tag==14) {
promptLabel.text = @"Fifteenth Label";
textEnterView.text = @" ";
[nextButton setTag:15];
//REAL METHOD
}
else if (nextButton.tag==15) {
promptLabel.text = @"Sixteenth Label";
conclusionString = textEnterView.text;
[nextButton setTag:16];
}
else if (nextButton.tag==16) {
promptLabel.text = @"Seventeenth Label";
textEnterView.text = @" ";
[nextButton setTag:17];
}
else if (nextButton.tag==17) {
[nextButton setTag:18];
}
else {
promptLabel.text = @"Eighteenth Label";
[nextButton setTag:1234];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
很明显,这些标签除了“第十八个标签”之外还说了些什么,但如果我说明为什么这么说,我就会放弃这个应用程序的想法。但是,所有其余代码都与应用程序中的完全相同。感谢您的帮助!
【问题讨论】:
-
我会先剔除假货。您可以简单地在修饰事件中更改标签。
-
我想我确实在修饰事件中更改了标签...我不明白为什么它一次执行两种方法,但如果你能详细说明一下,我会很乐意删除假货你是什么意思。我应该在哪里设置标签?你的意思是我将标签设置为 1234 最初应该发生在方法中,但在子方法之外(或任何你想调用的方法)而不是在 viewDidLoad 中?如果是这样,为什么这会让 iPhone/iPod 开始一次使用一个子方法而不是两个?谢谢!
-
因为您正在调用标签
1234或任何标签的方法,并且您在按钮仍然按下时更改标签,因此下一个方法被触发。我想如果你真的按住按钮足够长的时间,就会触发许多方法。如果在释放按钮时将标记的更改移动到动作触发中,则不应进行双重方法调用。堆满了假的方法调用,草率…… -
虽然这很有意义,但我只是尝试在我的 iPod Touch 5 上按住 UIButton 几次约 20 秒,它的行为仍然完全一样,所以我不认为这解释了这个问题......我仍然很困惑。不过感谢您的帮助!另外,如果您有答案,请将其发布为答案,而不是评论,以便我将您标记为正确并为您的麻烦提供代表点。
标签: ios multithreading nsstring uibutton uilabel