It is possible to use an existing ViewController w

2019-02-06 05:55发布

I use the method performSegueWithIdentifier:sender: to open a new ViewController from a storyboard-file programmatically. This works like a charm.

But on every time when this method is being called, a new ViewController would be created. Is it possible to use the existing ViewController, if it exista? I don't find anything about this issue (apple-doc, Stack Overflow, ...).

The Problem is: On the created ViewController the user set some form-Elements and if the ViewController would be called again, the form-elements has the initial settings :(

Any help would be appreciated.

Edit: I appreciate the many responses. Meanwhile, I'm not familiar with the project and can not check your answers.

7条回答
Ridiculous、
2楼-- · 2019-02-06 05:59

Use shouldPerforSegueWithIdentifier to either allow the segue to perform or to cancel the segue and manually add your ViewController. Retain a pointer in the prepareForSegue.

... header

@property (strong, nonatomic) MyViewController *myVC;

... implementation

-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if([identifier isEqualToString:@"MySegueIdentifier"]){
        if(self.myVC){
            // push on the viewController
            [self.navigationController pushViewController:self.myVC animated:YES];
             // cancel segue
            return NO; 
        }
    }
    // allow the segue to perform
    return YES;
}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"MySegueIdentifier"]){
        // this will only be called the first time the segue fires for this identifier
        // retian a pointer to the view controller
        self.myVC = segue.destinationViewController;
    }
}
查看更多
beautiful°
3楼-- · 2019-02-06 06:05

If it's a standard master-detail universal app (which uses a UISplitViewController) then it can this can be achieved by implementing shouldPerform as follows:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if([identifier isEqualToString:@"showDetail"]){
        if(self.detailViewController){
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            Event *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
            self.detailViewController.detailItem = object;
            return NO;
        }
    }
    return [super shouldPerformSegueWithIdentifier:identifier sender:sender];
}
查看更多
Animai°情兽
4楼-- · 2019-02-06 06:07

I faced this problem today and what I have done is to create the view controller manually and store it's reference. Then every time I need the controller, check first if exists. Something like this:

MyController *controller = [storedControllers valueForKey:@"controllerName"];

if (!controller)
{
    controller = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:NULL] instantiateViewControllerWithIdentifier:@"MyControllerIdentifierOnTheStoryboard"];

    [storedControllers setValue:controller forKey:@"controllerName"];
}

[self.navigationController pushViewController:controller animated:YES];

Hope it helps.

查看更多
Viruses.
5楼-- · 2019-02-06 06:10

Following code makes singleton view controller. Add them to your destination view controller implementation, then segue will reuse the same vc.

static id s_singleton = nil;
+ (id) alloc {
    if(s_singleton != nil)
        return s_singleton;
    return [super alloc];
}
- (id) initWithCoder:(NSCoder *)aDecoder {
    if(s_singleton != nil)
        return s_singleton;
    self = [super initWithCoder:aDecoder];
    if(self) {
        s_singleton = self;
    }
    return self;
}
查看更多
淡お忘
6楼-- · 2019-02-06 06:15

Create a property for the controller.

@property (nonatomic, weak) MyController controller;

And use some kind of lazy initialization in performSegueWithIdentifier:sender

if (self.controller == nil)
{
self.controller = [MyController alloc] init]
...
}

In this case, if controller was already created, it will be reused.

查看更多
一纸荒年 Trace。
7楼-- · 2019-02-06 06:18

To reuse an existing UIViewController instance with a segue create the segue from scratch and provide your own (existing) destination (UIViewController). Do not forget to call prepareForSegue: if needed.

For example:

UIStoryboardSegue* aSegue = [[UIStoryboardSegue alloc] initWithIdentifier:@"yourSegueIdentifier" source:self destination:self.existingViewController]
[self prepareForSegue:aSegue sender:self];
[aSegue perform];
查看更多
登录 后发表回答