开发者

How to get a return value from a ViewController? // popViewControllerAnimated twice? Double back?

I have included the Three20 Library and use the TTNavigator for my views.

My goal is to flip from view 开发者_如何学运维4 back to view 2.

The only solution I found for this, is to call popViewControllerAnimated in View 4 to get to View 3, then in the ViewDidAppear in view 3 call the popViewControllerAnimated again, to get to View 2.

Problem is, of course, I only want to call the popViewControllerAnimated in View 3 ViewDidAppear only, when the ViewDidAppear is called from View 4 to View 3, not in other cases (e.g. View 2 opens View 3).

So as far as I see I have to set a BOOL property or something like this, but how? I can't work with delegates here, because of the URL-Navigation given by Three20.


Use UINavigationController's -popToViewController:animated: method:

// This is a method of your UIViewController subclass with view 4
- (void) popTwoViewControllersAnimated:(BOOL)animated {
    NSInteger indexOfCurrentViewController = [self.navigationController.viewControllers indexOfObject:self];
    if (indexOfCurrentViewController >= 2)
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:indexOfCurrentViewController - 2] animated:animated];
}


What about using the Singleton design pattern

You can set a bool in there that you can change depending from which view you are coming and then in view 3 check what that value is. In my opinion its the easiest way of doing this without using the delegates

.h file

@interface Singleton : NSObject
{
     BOOL flag;
}

@property(nonatomic) BOOL flag;
@end


.m file

static Singleton *instance = nil;
@implementation Singleton
@synthesize flag;
+(id) sharedManager
{
    @synchronized(self){
        if(instance == nil)
             instance = [[super allocWithZone:NULL] init];
    }
    return instance;
}

+(id)allocWithZone:(NSZone *)zone
{
    return [[self sharedManager] retain];
}

-(id) copyWithZone:(NSZone *)zone
{
    return self;
}

-(id) retain
{
    retrun self;
}

-(unsigned) retainCount
{
    retrun 1;
}

-(id)  init
{
    if(self == [super init])
    {
        flag = NO;
    }
}

Forgive me if there is some small mistake in the code, did it out the top of my head real quick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜