Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

objective c - Simple Passing of variables between classes in Xcode

I am trying to do an ios app but I am stuck at passing data between classes . This is my second app . The first one was done whit a global class , but now I need multiple classes . I tried many tutorials , but did not work or the passed value was always zero . Can someone please write me a simple app to demonstrate passing of variables in IOS 5 . Nothing special , storyboard whith 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 the indexPath.row or the index of the cell that i pressed to the next view

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are several things to do. Depending on the app, you could either add a variable to the AppDelegate class, making it availible to all classes through a shared instance. The most common thing (I think) is to make a singleton. For that to work, you can make a class, say StoreVars, and a static method that returns the object, which makes the class "global". Within the method, you initialize all your variables, like you always would. Then you can always reach them from wherever.

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

This will make a singleton. If you remember to import "StoreVars.h" in your two viewControllers, you can access the now shared array like this;

[StoreVars sharedInstance].mySharedArray;
               ^

This is a method returning a StoreVars object. Within the StoreVars class, you can implement any object and initialize it in the static method. Just always remember to initialize it, or else, all your object will be 0/nil.

If you are not a fan of the UINavigationController and would rather use segues, it's a lot easier, but can make your app rather "messy" imo. There is a method implemented in UIViewController you should overload:

- (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: How to pass prepareForSegue: an object

Do some research before asking questions like this. Read some tutorials, and try out yourself, and then ask questions related to what you are really looking for. It's not everyday people want to do all the work for you, but sometimes you're lucky. Like today.

Cheers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...