Simple Passing of variables between classes in Xco

2019-01-25 21:59发布

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

3条回答
手持菜刀,她持情操
2楼-- · 2019-01-25 22:31

The problem you face is quite perplexing for beginners. "Solving" it wrong way can result in learning a ton of bad habits.
Please have a look at Ole Begemann's excellent tutorial on Passing Data Between View Controllers - it's really worth reading.

查看更多
贼婆χ
3楼-- · 2019-01-25 22:32

if you use segue between 2 controllers you must overload prepareToSegue method

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// check if it's 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
}
}
查看更多
虎瘦雄心在
4楼-- · 2019-01-25 22:39

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.

查看更多
登录 后发表回答