2016-11-14 21 views
0

有兩類DropDownPickereditAddVehicle類。只需要在editAddVehicle中聲明一個名稱爲「brandID1」的字符串變量,我希望它從DropDownPicker類中提取數據。傳遞和從其他類中提取數據時顯示爲空

這裏是我的代碼:

DropDownPicker.h

@property(nonatomic,retain) NSArray *selectedIndex; 

DropDownPicker.m

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 

    if ([[dataArrayForPicker objectAtIndex:row] isKindOfClass:[NSString class]]){ 
     NSLog(@"Selected %@. Index is: %i brand id %@" , [dataArrayForPicker objectAtIndex:row], row,_selectedIndex[row]); 
     editAddVehicle *controller=[[editAddVehicle alloc]init]; 
     controller.brandID1=_selectedIndex[row]; 
     NSLog(@"Brand Id %@",controller.brandID1); 
     // Brand Id 50 
     self->pickerTextFeild.text = [dataArrayForPicker objectAtIndex:row]; 
    } 
    else 
     self->pickerTextFeild.text = [dataArrayForPicker objectAtIndex:row]; 
} 

DropDownPicker通過此行controller.brandID1=_selectedIndex[row];的代碼很容易我已存儲的字符串在crandID1但爲什麼它的聖生病editAddVehicle.m

EditAddVehicle.h

@interface editAddVehicle : UIViewController 
@property(nonatomic,retain)NSString *brandID1; 
@end 

EditAddVehicle.m

@interface editAddVehicle()<UITextFieldDelegate> 
{ 
    NSString *brandID1; 
} 
@end 

@implementation editAddVehicle 
@synthesize brandID1; 
{ 
    UITextField *textFeild1=(UITextField *)[self.view viewWithTag:10]; 
    brand= [[DropDownPicker alloc]initWithTextFeild:textFeild1 withData:brandArray]; 
    [brand setPlaceHolder:@"Brand"]; 
    brand.selectedIndex=brandCode; 
    [brand addTarget:self action:@selector(brandSelected:) forControlEvents:UIControlEventValueChanged]; 
} 

-(void)brandSelected:(DropDownPicker *)sender{ 
    NSLog(@"%@",brandID1); 
    // (null) why ? 
} 

回答

1

這種錯誤的方式來傳遞你需要用戶委託或NSNotificationCenter出於傳遞數據。

這裏我對NSNotificationCenter

//例如,在DropDownPicker

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
    { 
    if ([[dataArrayForPicker objectAtIndex:row] isKindOfClass:[NSString class]]){ 
     NSLog(@"Selected %@. Index is: %i brand id %@" , [dataArrayForPicker objectAtIndex:row], row,_selectedIndex[row]); 
     [[NSNotificationCenter defaultCenter]postNotificationName:@"NotificationForData" object:_selectedIndex[row]]; 

    } 
     else{ 
self->pickerTextFeild.text = [dataArrayForPicker objectAtIndex:row]; 
// un comment if you want pass data here 
// [[NSNotificationCenter defaultCenter]postNotificationName:@"NotificationForData" object:_selectedIndex[row]]; 
     } 

} 

IN editAddVehicle //在viewDidLoad中

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(brandSelected:) name:@"NotificationForData" object:nil]; 

-(void)brandSelected:(NSNotification*)noti{ 
    NSString *brandID1 = [noti object]; 
    NSLog(@"%@",brandID1); 
} 
+0

那麼想你的建議的方式太先生,但是力工作一樣了事情無效反映。 –

+0

你檢查了[noti object]; ? – KKRocks

+0

ooh對不起!它的工作非常感謝這個寶貴的建議或答案。 –

相關問題