如何使用从另一个方法中的一个方法计算的值来调用变量?

时间:2011-12-28 19:46:02

标签: iphone objective-c ios call

我的代码在“IBAction”按下按钮时进行计算,并将结果作为“UIAlertView”中的“消息”返回字符串。

    else{

    NSString *str = [[NSString alloc] initWithFormat:@"You require an average GPA of at least %.2f to achieve your Goal of %@", gpagoal, (NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Nothing is Impossible" 
                                                    message:str 
                                                   delegate:self 
                                          cancelButtonTitle:@"Good Luck" 
                                          otherButtonTitles:@"Tweet",nil];

    //show alert
    [alert show];
    [alert release];
    NSLog(@"All Valid");
}

我遇到了如何从计算的“IBAction”方法中提取“gpagoal”值的问题。

下面是推文独立按钮的代码,如果我能够从其他方法移植gpagoal值,则该代码有效。

- (IBAction)sendEasyTweet:(id)sender {
// Set up the built-in twitter composition view controller.
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];

// i have problem trying to pull the result "gpagoal" from the calculation's "IBAction" method as i dunno how to pull out variable from a method to another method.
NSString *str2 = [[NSString alloc] initWithFormat:@"I need an average GPA of at least %.2f this semester to achieve my Goal of %@", gpagoal, (NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]]; 

// Set the initial tweet text. See the framework for additional properties that can be set.
[tweetViewController setInitialText:str2];

// Create the completion handler block.
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
    //NSString *output;

    switch (result) {
        case TWTweetComposeViewControllerResultCancelled:
            // The cancel button was tapped.
            //output = @"Tweet cancelled.";
            NSLog(@"Tweet cancelled");
            break;
        case TWTweetComposeViewControllerResultDone:
            // The tweet was sent.
            //output = @"Tweet done.";
            NSLog(@"Tweet done");
            break;
        default:
            break;
    }

    // Dismiss the tweet composition view controller.
    [self dismissModalViewControllerAnimated:YES];
}];

// Present the tweet composition view controller modally.
[self presentModalViewController:tweetViewController animated:YES];

}

1 个答案:

答案 0 :(得分:0)

您只需要一个可在整个班级中使用的变量

// ViewController.h

...
@interface ViewController : UIViewController {
   double gpagoal;
}

确保根据需要正确初始化和更新。