ios - 从其他类传递和获取数据时显示 null

标签 ios objective-c class uiviewcontroller

有两个类 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 处仍显示 null?

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 ?
}

最佳答案

这种错误的数据传递方式需要用户委托(delegate)或 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]];
        }

}

在 editAddVehicle 中 //在viewdidload中

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

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

关于ios - 从其他类传递和获取数据时显示 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40584426/

相关文章:

ios - 两个不同层上的两个 CABasicAnimation 同时进行

ios - 核心蓝牙状态恢复

ios - 什么是 setTranslatesAutoresizingMaskIntoConstraints :NO for?

ios - Swift 3 - UIView 并不总是显示其内容

ios - 添加 Flurry 库后的链接器错误

ios - 分析显示循环的每次迭代中都有泄漏

objective-c - 如何在 objective-c 中实现链表?

java - 当类中实际上有一个 main 方法时,没有 main 方法

c# - 无数据类型的开销是多少?

python - 在 python 中 __init__ 字典参数默认为以前的类值