iOS初学者:Tweet计算结果(如何将结果添加到NSString并Tweet它)



我正试图将twitter框架集成到我的应用程序中,以给用户提供选项推特他们的计算结果。推特工作得很好,但我无法得到结果到Twitter窗口。

  1. 用户输入高尔夫球场所需的洞数和杆数。
  2. App计算每洞平均冲程。
  3. 点击推特结果按钮后,推特窗口出现并已经计算平均值作为twitter消息的一部分。

现在Twitter窗口显示,但我只能将静态文本设置为默认值。

有人能帮忙吗?

下面是我的代码:

计算:(工作正常)

- (IBAction)calculateNow:(id)sender {
    double holes = [[holesTextField text]doubleValue];
    double strokes = [[strokesTextField text]doubleValue];
    double result = strokes / holes;
    [averageTextField setText:[NSString stringWithFormat:@"%.2f", result]];
}

TWITTER:(工作,但窗口不包括计算平均值)

- (IBAction)tweetMyAverage:(id)sender {
    if ([TWTweetComposeViewController canSendTweet]) {
        TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc]init];
        [tweetSheet setInitialText:[NSString stringWithFormat:@"My Average is: %@", result]];
        [self presentModalViewController:tweetSheet animated:YES];
    }
}

我认为变量的结果(从计算)不是在twitter IBAction中知道,但我如何使它可用?

@interface部分的.h文件中添加一个变量:

@interface MyClass : UIViewController {
    double result; 
}
@end

那么它将可以在每个方法的.m中使用。

从您在calculateNow方法中创建的实例变量中删除double类,以确保您在创建单独的变量时不会收到警告。

result = strokes / holes;

另外,确保您使用%d用于双精度,%@用于字符串。

[tweetSheet setInitialText:[NSString stringWithFormat:@"My Average is: %d", result]];

假设你提供的所有代码都在同一个视图控制器中,你可以这样做:

     [tweetSheet setInitialText:[NSString stringWithFormat:@"My Average is: %@",
     averageTextField.text]];

最新更新