【问题标题】:How to call a variable with a value calculated from one method in another method?如何调用具有从另一种方法中的一种方法计算的值的变量?
【发布时间】:2011-12-28 19:46:01
【问题描述】:

我的代码在“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];

}

【问题讨论】:

    标签: iphone objective-c ios call


    【解决方案1】:

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

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

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

    【讨论】:

    • 其实我之前尝试过在视图控制器的界面中添加“double gpagoal”的方法。然后我收到一个警告-'gpagoal'的本地声明隐藏了实例变量。阅读您的答案后,我认为我收到警告的原因是因为我没有初始化变量。那么我应该如何初始化它并更新变量?谢谢。
    • viewDidLoad 中设置pgagoal = 1.0;。每当控制器从视图或模型中获取pgagoal 已更改的信息时,将gpagoal 设置为新值。
    • 好的 viewdidload,我应该输入什么? GPAMainViewController *gpagoal = 0; ?
    • 请阅读我的评论。我给了你整条线。双精度不是指针,所以请去掉星号。
    • 甜,它有效!所以我在 view controller.h 的接口中声明了一个新的全局变量调用 pgagoal,然后我在 viewdidload 中将它设置为等于 1.0。然后当 gpagoal 值发生变化时,我设置 pgagoal = gpagoal 将新值传递给它。当我想在其他方法中使用值(gpagoal)时,我调用全局变量 pgagoal。这解决了侧面问题非常感谢!你有让其他标题按钮发送推文的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 2020-06-18
    • 2014-12-18
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    相关资源
    最近更新 更多