Simple Passing Variables Between Classes in Xcode

I'm trying to make an ios application, but I'm stuck in passing data between classes. This is my second application. The first was made with a global class, but now I need some classes. I tried many tutorials, but did not work, or the passed value was always zero. Can someone please write me a simple application to demonstrate the transfer of variables in iOS 5. Nothing special, storyboard with 2 view controllers, one variable.

Thank you for your help.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.

            FirstViewController *fv;
            fv.value = indexPath.row;

            NSLog(@"The current %d", fv.value);

            FirstViewController *detail =[self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
            [self.navigationController pushViewController:detail animated:YES]; 

}

here is the code from my main view, and I need to send indexPath.row or the index of the cell that I clicked on the next view

+3
source share
3 answers

, . AppDelegate, . ( ) - . , StoreVars, , , "". , . .

@interface StoreVars : NSObject

@property (nonatomic) NSArray * mySharedArray;
+ (StoreVars*) sharedInstance;

@implementation StoreVars
@synthesize mySharedArray;

+ (StoreVars*) sharedInstance {
    static StoreVars *myInstance = nil;
    if (myInstance == nil) {
        myInstance = [[[self class] alloc] init];
        myInstance.mySharedArray = [NSArray arrayWithObject:@"Test"];
    }
    return myInstance;
}

. "StoreVars.h" viewControllers, , :

[StoreVars sharedInstance].mySharedArray;
               ^

, StoreVars. StoreVars . , , 0/.

UINavigationController segues, , "" imo. UIViewController , :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        YourViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];
    }
}

source: prepareForSegue:

, . , , , . , , . .

.

+12

segue , prepareToSegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// check if it the good segue with the identifier
if([[segue identifier] isEqualToString:@"blablabla"])
{
    // permit you to get the destination segue
    [segue destinationViewController];
    // then you can set what you want in your destination controller
}
}
+1

The problem you are facing is quite confusing for beginners. "Solving" this wrong path can lead to learning a ton of bad habits.
Please see Ole Begemann's excellent tutorial on Transferring Data Between Viewers - It's Really Worth Reading.

+1
source

All Articles