Harshit Goel Harshit Goel - 23 days ago 8
iOS Question

showing null while passing & fetching data from other class

There are two classes

DropDownPicker
and
editAddVehicle
Class. Simply I have declare a string variable in
editAddVehicle
with name "
brandID1
" and I want it to bring data from
DropDownPicker
Class.

Here is my code:

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];
}


From
DropDownPicker
through this line
controller.brandID1=_selectedIndex[row];
of code easily I have stored string in
crandID1
but why its still showing null at
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 ?
}

Answer

This wrong way to pass data you need to user delegate or NSNotificationCenter for pass data .

Here MY EXample for NSNotificationCenter

// IN 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 // in viewdidload

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

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