Ios 带有两个按钮的Xcode应用程序。当我单击其中一个时,将触发这两个操作的操作。为什么会这样?
这是一个示例Twitter应用程序,是我在苹果开发者网站上根据教程制作的。但我不知道我做错了什么才让这一切发生 接口:Ios 带有两个按钮的Xcode应用程序。当我单击其中一个时,将触发这两个操作的操作。为什么会这样?,ios,objective-c,xcode,twitter,Ios,Objective C,Xcode,Twitter,这是一个示例Twitter应用程序,是我在苹果开发者网站上根据教程制作的。但我不知道我做错了什么才让这一切发生 接口: @interface TWTViewController : UIViewController { NSString* output; } @property (nonatomic, copy) NSString* output; - (IBAction)doTweet:(id)sender; - (IBAction)getTimeline:(id)sender; @prope
@interface TWTViewController : UIViewController {
NSString* output;
}
@property (nonatomic, copy) NSString* output;
- (IBAction)doTweet:(id)sender;
- (IBAction)getTimeline:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;
@property (weak, nonatomic) IBOutlet UIButton *tweetButton;
@end
实施:
@implementation TWTViewController
@synthesize output = _output;
@synthesize outputLabel;
@synthesize tweetButton;
...
- (IBAction)doTweet:(id)sender {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:@"It's really that simple!"];
[twitter addImage:[UIImage imageNamed:@"twitter.png"]];
[self presentViewController:twitter animated:YES completion:nil];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Your Tweet was posted succesfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
else if(res == TWTweetComposeViewControllerResultCancelled) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your Tweet was not posted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
[self dismissModalViewControllerAnimated:YES];
};
}
- (IBAction)getTimeline:(id)sender {
ACAccountStore* store = [[ACAccountStore alloc] init];
ACAccountType* twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[store requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
NSArray* twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if([twitterAccounts count] > 0) {
ACAccount* account = [twitterAccounts objectAtIndex:0];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:@"1" forKey:@"include_entities"];
NSURL* url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];
TWRequest* request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(error != nil) {
self.output = [error localizedDescription];
self.outputLabel.text = self.output;
}
else {
NSError* jsonError;
NSArray* timeline = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError];
if(jsonError == nil) {
self.output = [timeline componentsJoinedByString:@"|"];
self.outputLabel.text = self.output;
}
else {
self.output = [jsonError localizedDescription];
self.outputLabel.text = self.output;
}
}
}];
}
}
}];
}
@end
以下是包含整个项目的ZIP文件:
任何帮助都将不胜感激。检查IB中的所有连接。我知道这听起来很愚蠢,但它总是让我感到困惑。当前,“获取时间线”按钮设置为同时启动
doTweet:
和getTimeline:
。右键单击IB中的Get Timeline按钮,您将在Sent Events->Touch Up Inside下看到这两个按钮。单击doTweet:
旁边的小X,您应该一切正常
@Szwedo的建议很好;始终检查IB中的连接和操作。什么样的帮助?你做了什么?有什么问题?为什么会有人下载整个项目?@Sarah问题:我点击按钮1。按钮1的操作被触发。按钮2的操作也会触发。我需要帮助找出原因。我没有发现代码有任何错误,这就是为什么我分享了这个项目,认为更有经验的人可以发现错误。这是一个非常小的项目,只下载了几百KBs。您的项目缺少帐户和twitter框架。给出了相同的错误。只需在IOS设备上运行。无配置文件。无法测试它。你能在这方面做些什么吗?@Sarah Accounts.Framework和Twitter.Framework是iOS5 SDK的一部分。它们可以在文件夹/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/Frameworks中找到。什么是“供应概况”?(我现在已经在原来的帖子中添加了代码。)是的,这很有帮助!这在故事板文件中:谢谢!现在我明白了问题所在。固定的