将变量从一个函数传递给另一个函数

时间:2013-03-04 18:39:14

标签: objective-c variables

我有一个问题..我想从此函数传递选定的segmentet索引变量:

-(IBAction)segControllIndex:(id)sender {

    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
   int segValue = [segmentedControl selectedSegmentIndex];

到这里:

 NSString *strURL = [NSString stringWithFormat:
 @"http://phpFile.php?Name=%1@&Artist=%2@&Titel=%3@&Genere=%4@", 
 insertName,insertArtist,insertTitle, PASSED VALUE];

我尝试了很多东西,但我已经成功了.. 你能救我吗?

非常感谢...

1 个答案:

答案 0 :(得分:0)

假设类型应该是整数,假定insertNameinsertArtistinsertTitleNSString的实例:

// define this method in your class
-(void)someMethod:(int)passedValue {
    NSString *strURL = [NSString stringWithFormat:@"http://phpFile.php?Name=%@&Artist=%@&Titel=%@&Genere=%d", insertName,insertArtist,insertTitle, passedValue];

    // do your work
}

这样,您可以致电:

-(IBAction)segControllIndex:(id)sender {
    // I would recommend validating 'sender' first
    // withou knowing anything else about your calls, here is one example
    if ( [sender isKindOfClass:[UISegmentedControl class]] ) {
        UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
        int segValue = [segmentedControl selectedSegmentIndex];

        [self someMethod:segValue];
    }
}

我建议reading up on how to format NSStringprimer on Objective C methods and messaging