2013-03-04 101 views
0

我有一个问题..我想选择的segmentet索引变量从这个函数传递:传递变量从一个到另一个功能

-(IBAction)segControllIndex:(id)sender { 

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

到这里:

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

我tryed这么多事情,但我havend成功.. 你能帮我吗?

感谢这么多...

+0

传递的值是int..so使用%d,编写完整的函数,你试过了。 – Guru 2013-03-04 18:42:54

+1

阅读Objective-C的基础知识,因为这很简单。即使你的第一个方法传递参数(发件人)... – msgambel 2013-03-04 18:43:51

+0

我想通过segValue其他功能... – 2013-03-04 18:48:03

回答

0

假设类型应该是整数,并假设insertNameinsertArtist,并insertTitleNSString实例:

// 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 NSString,一个primer on Objective C methods and messaging

+0

你最后的例子非常帮助我在其他部分..谢谢! – 2013-03-04 22:30:40

+0

@CarstenGraf很高兴我很聪明。 – 2013-03-04 22:37:52

相关问题