Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 可以将现有的ViewController与PerformsgueWithIdentifier一起使用?_Objective C_Ios5_Uiviewcontroller_Storyboard - Fatal编程技术网

Objective c 可以将现有的ViewController与PerformsgueWithIdentifier一起使用?

Objective c 可以将现有的ViewController与PerformsgueWithIdentifier一起使用?,objective-c,ios5,uiviewcontroller,storyboard,Objective C,Ios5,Uiviewcontroller,Storyboard,我使用方法performsguewithidentifier:sender:以编程方式从情节提要文件打开一个新的ViewController。这很有魅力 但每次调用此方法时,都会创建一个新的ViewController。如果现有的ViewController,是否可以使用?我找不到有关此问题的任何信息(苹果文档、堆栈溢出等) 问题是: 在创建的ViewController上,用户设置一些表单元素,如果再次调用ViewController,表单元素具有初始设置:( 任何帮助都将不胜感激 编辑: 感

我使用方法
performsguewithidentifier:sender:
以编程方式从情节提要文件打开一个新的
ViewController
。这很有魅力

但每次调用此方法时,都会创建一个新的
ViewController
。如果现有的
ViewController
,是否可以使用?我找不到有关此问题的任何信息(苹果文档、堆栈溢出等)

问题是: 在创建的
ViewController
上,用户设置一些表单元素,如果再次调用
ViewController
,表单元素具有初始设置:(

任何帮助都将不胜感激

编辑:
感谢您的回复。同时,我对项目不熟悉,无法检查您的答案。

为控制器创建属性

@property (nonatomic, weak) MyController controller;
并在
performsguewithidentifier:sender

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

在这种情况下,如果控制器已经创建,它将被重用。

我今天遇到了这个问题,我所做的是手动创建视图控制器并存储其引用。 然后每次我需要控制器时,首先检查是否存在。 大概是这样的:

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];

希望有帮助。

要使用segue重用现有的
UIViewController
实例,请从头开始创建segue并提供您自己的(现有)目标(
UIViewController
)。如果需要,请不要忘记调用
prepareforsgue:

例如:

UIStoryboardSegue* aSegue = [[UIStoryboardSegue alloc] initWithIdentifier:@"yourSegueIdentifier" source:self destination:self.existingViewController]
[self prepareForSegue:aSegue sender:self];
[aSegue perform];

下面的代码使单例视图控制器。 将它们添加到目标视图控制器实现中,然后segue将重用相同的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;
}

您需要将Viewcontroller设置为singleton类。

使用shouldPerforSegueWithIdentifier允许segue执行或取消segue并手动添加Viewcontroller。在prepareForSegue中保留指针

…标题

@property (strong, nonatomic) MyViewController *myVC;
…实施

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

首先,你会反对苹果的设计:“一个segue总是呈现一个新的视图控制器”

为了理解为什么segue所做的是创建一个新的视图控制器,然后根据segue的类型执行调用showViewController或showDetailViewController。因此,如果您有一个现有的视图控制器,只需调用这些方法即可

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Event *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    self.detailViewController.detailItem = object;
    [self showDetailViewController:self.detailViewController.navigationController sender:self];
}

根据MVC模式,您应该将用户值保存在任何共享对象或NSUserDefaults中。因此,您的问题并不完全正确。您是对的,此功能目前尚未实现,将来还会出现!这将解决我的问题,但问题仍然是一样的,如果可以使用此方法重用ViewController?我不能假设这是不可能的。谢谢,我已经检查过了,但它对我不起作用。我不知道在执行方法的上下文中惰性初始化是如何工作的?谢谢你的建议,这个解决方案似乎会起作用。我通过保存用户输入解决了我的问题,如d.lebedev所述。你在na的帮助下推送viewcontrollervigationController,你能改用PerformsgueWithIdentifier方法吗?你如何用这种方法处理
didReceiveMemoryWarning
?你救了我!谢谢:)使用这种方法,segue的其他属性从何而来,例如演示样式?这是UIStoryboardSegueTemplate _performWithDestinationViewController为您所做的,只是如果设置了segue.animates,它也会保留发送方并设置UIView animationEnabled。这被认为是达到并改变父状态的错误做法