Rocky
Rocky

Reputation: 1423

how to send data backword from second class to first class in iphone

i am try to exchange the one cell to the value of other cell but my value is not transfer proper bec my value is passing second class to first class but not string value is passing array reference why what is the mistake in my code can u help me for this i create delegate method this is my originstart.h file

import

#import "RegexKitLiteDemoAppDelegate.h"
@class airport;
@protocol OriginstartDelegate <NSObject>
@required
- (void)Originstart:(NSString *)Origin forIndexPath:(NSIndexPath *)indexPath;

@end

@interface Originstart : UITableViewController {
    RegexKitLiteDemoAppDelegate *app;
    airport *selectedAirport;
}
@property(nonatomic,retain)NSString*name;
@property(nonatomic,retain)airport *selectedAirport;
@property (nonatomic, copy) NSIndexPath *indexPathToChange;
@property (nonatomic, assign) id<OriginstartDelegate> delegate;
@end

this is my orginestart.m file

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return app.lstAirports.count;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    airport* a=(airport*)[app.lstAirports objectAtIndex:indexPath.row];
    NSLog(@"str:%@",a);
    cell.textLabel.text =a.Name;
    cell.detailTextLabel.text=a.Code;
    // Configure the cell...

    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *newStation = [[app.lstAirports objectAtIndex:indexPath.row]description];
    [delegate Originstart:newStation  forIndexPath:self.indexPathToChange];
    [self.navigationController popViewControllerAnimated:YES];

                 }

this is my Tfirst.m file

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"accessory selected");


    if ([indexPath section] == 0)
    {
       // load the appropriate view for the accessory selected
        if ([indexPath row] == 0)
        {

            Originstart *viewtwo = [[Originstart alloc] initWithStyle:UITableViewStyleGrouped];
            viewtwo.indexPathToChange = indexPath;
            viewtwo.delegate = self;
            [self.navigationController pushViewController:viewtwo animated:YES];
            [viewtwo release];

        }
        else{
            NSLog(@"accessory right");
            }       
    }
}


#pragma mark - Delegate protocol method

- (void)Originstart:(NSString *)Origin forIndexPath:(NSIndexPath *)indexPath {

    [datasource replaceObjectAtIndex:indexPath.row withObject:Origin];
    [self.tableView reloadData];
}

this is my airport.h file where i create two string variable and one array for take value and display for in originstart classwith name and code i writen this in here in class

originstart.m  airport* a=(airport*)[app.lstAirports objectAtIndex:indexPath.row];
    NSLog(@"str:%@",a);
    cell.textLabel.text =a.Name;
    cell.detailTextLabel.text=a.Code;
and final this class arport class

//
//  airport.h
//  RegexKitLiteDemo
//
//  Created by pradeep.yadav on 9/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface airport : NSObject {
    NSString *Code;
    NSString *Name;
    NSArray *DestinationAirports;

}
@property(nonatomic,retain)NSString *Code;
@property(nonatomic,retain)NSString *Name;
@property(nonatomic,retain)NSArray *DestinationAirports;
@end

Upvotes: 0

Views: 164

Answers (2)

Ankit Sachan
Ankit Sachan

Reputation: 7840

The simplest apprroach is to set an object in FirstViewController here is the way

SecondViewController

FirstViewController *instanceOfFirstView;

@property (nonatomic, retain) FirstViewController *instanceOfFirstView;

@synthesize instanceOfFirstView;

[instanceOfFirstView setValueNeedTobeBroughtFromSecondView:@"VAlue set"]

FirstViewController

NSString *valueNeedTobeBroughtFromSecondView;

@property (nonatomic, retain) NSString *valueNeedTobeBroughtFromSecondView;

@synthesize valueNeedTobeBroughtFromSecondView;

SecondViewController *aSecondViewController=[[SecondViewController alloc] init];

[aSecondViewController setInstanceOfFirstView: self];

Upvotes: 0

stack2012
stack2012

Reputation: 2186

To achieve what u want , just try these steps:

1.First, create a property of first class in the second class.

2.In the first class create those variables or objects as properties to which u want to pass the data from the second class.

3.(Hope u are using navigation controller based app ) While pushing from the first to second view controller u create a instance of the second class and pass it pushViewController method. Add the following line of code along with it.....

secondViewObj.firstView=self; 

where secondViewObj is the instance of the secondViewController class and firstView is the property declared in that second class.

4.Now in the second class when u want to pass the data to first class just add the following line of code:

firstView.dataMember1=value;

where dataMember1 is the variable or object which was created as a property as mentioned in the 2nd point. Hope this helps.

In the FirstViewController class where u push to SecondviewController have the following piece of code:

    SecondViewController *svc = [SecondViewController alloc]init];
    svc.vc = self; 
    [self.navigationController pushViewController:svc animated:YES];

and also have a property declared for the data u want to pass to the firstView Controller.

In this case it s going to be a integer variable.

@property (nonatomic) int selectedValue and also synthesize it.

Now in the secondViewController , declare a property for the firstViewController like this

@property (nonatomic, retain) FirstViewController *vc; and synthesize it.

Where u want to pass the data to the FirstViewController have the following code,

vc.selectedValue = 1;

Now after popping back to the firstViewController , if u chk the value of the selectedValue variable u will find the value has been passed from second to first.

Hope this helps..

Upvotes: 1

Related Questions