Search code examples
iphoneios5uiviewcontrolleruiactionsheet

Not able pass the data from one view to another in iphone


I have included UIActionSheet in my program.It contains 2 button say A and B. When a user clicks on the A button it goes to another view. In that view, it contains 3 text field.So in the mainviewcontroller, I am assigning the values to these textfield. I will paste the code below :-

In MainViewController.m

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex 
  {
         AnotherViewController *A = [[AnotherViewController alloc] init];

          if (buttonIndex == 1) 
          {

              A.newtask = name;
              A.newsubtask = sub1;
              A.newduedate = dateName;
              A.newtime = timeName;


     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];                                                                    

     UpdateViewController   *viewController =[storyboard instantiateViewControllerWithIdentifier:@"update"];

     [self presentViewController:viewController animated:YES completion:NULL];


          }
   }

And in AnotherViewcontroller.m

 - (void)viewDidLoad
   {

              task_update.text = newtask;
              subtask_update.text =newsubtask;
              duedate_update.text = newduedate;
              time_update.text = newtime;

           NSLog(@"The task is :%@",newtask);
              [super viewDidLoad];
// Do any additional setup after loading the view.
    }

The problem is I am getting null value in the newtask. Can anyone please tell me the solution. :(


Solution

  • Take Global Variables in AppDelegate Class. Like AppDelegate .h

    NSString *newtask,*newsubtask,*newduedate,*newtime;
    
    @property(nonatomic,retain)NSString *newtask,*newsubtask,*newduedate,*newtime;
    

    AppDelegate. m

    @synthesize newtask,newsubtask,newduedate,newtime;
    

    MainViewController.h

    import "AppDelegate"

    {
    AppDelegate *appDelegate;
    }
    

    MainViewController.m

        (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex 
          {
    appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
                 AnotherViewController *A = [[AnotherViewController alloc] init];
        
                  if (buttonIndex == 1) 
                  {
        
                      appDelegate.newtask = name;
                      appDelegate.newsubtask = sub1;
                      appDelegate.newduedate = dateName;
                      appDelegate.newtime = timeName;
        
        
             UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];                                                                    
        
             UpdateViewController   *viewController =[storyboard instantiateViewControllerWithIdentifier:@"update"];
        
             [self presentViewController:viewController animated:YES completion:NULL];
        
        
                  }
           }
    

    AnotherViewcontroller.h

    import "AppDelegate"

    {
    AppDelegate *appDelegate;
    }
    

    AnotherViewcontroller.m

    - (void)viewDidLoad
       {
        appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
                  task_update.text = appDelegate.newtask;
                  subtask_update.text =appDelegate.newsubtask;
                  duedate_update.text = appDelegate.newduedate;
                  time_update.text = appDelegate.newtime;
    
               NSLog(@"The task is :%@",newtask);
                  [super viewDidLoad];
    // Do any additional setup after loading the view.
        }