iOS dev - UINavigationController:向后发送数据到第二个父视图

时间:2011-12-03 19:04:02

标签: ios uinavigationcontroller

所以,我需要在弹出两个视图时向后发送数据。我尝试使用协议,所以这是我的代码的一部分:

RootViewController.h

#import <UIKit/UIKit.h>
#import "ThirdViewController.h"

@class SecondViewController;

@interface RootViewController : UIViewController <PassInt>

@property (strong, nonatomic) SecondViewController *secondViewController;
@property (strong, nonatomic) ThirdViewController *thirdViewController;
@property (nonatomic) int intNumber;

@end

RootViewController.m

#import "RootViewController.h"
#import "SecondViewController.h"

@implementation RootViewController

@synthesize secondViewController = _secondViewController;
@synthesize thirdViewController = _thirdViewController;
@synthesize intNumber = _intNumber;

- (void)setIntNumber:(int)number{
    _intNumber = number;
}

#pragma mark - View lifecycle

- (void)viewWillAppear:(BOOL)animated {
    if(!_thirdViewController){
        _thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:[NSBundle mainBundle]];
    }
    if(!_secondViewController){
        _secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
        self.secondViewController.thirdViewController = self.thirdViewController;
    }
    [self.navigationController pushViewController:self.secondViewController animated:YES];
    }

@end

SecondViewController.h

#import <UIKit/UIKit.h>

@class ThirdViewController;

@interface SecondViewController : UIViewController

@property (strong, nonatomic) ThirdViewController *thirdViewController;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "ThirdViewController.h"

@implementation SecondViewController

@synthesize thirdViewController = _thirdViewController;

...

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
        [self.navigationController pushViewController:self.userPreferencesViewController animated:YES];
}
@end

ThirdViewController.h

#import <UIKit/UIKit.h>

@protocol PassInt <NSObject>
@required
- (void) setIntNumber:(int)number;
@end

@interface ThirdViewController : UIViewController{
id <PassInt> delegate;
}
@property (retain) id delegate;
- (IBAction)saveChanges;

@end

ThirdViewController.m

#import "ThirdViewController.h"

@implementation ThirdViewController

- (IBAction)saveChanges{
    int someInt = 3;
    [[self delegate] setIntNumber:someInt];
    UINavigationController *tempNavigationController = self.navigationController;
    [tempNavigationController popViewControllerAnimated:NO];
    [tempNavigationController popViewControllerAnimated:NO];
}
...
@end

我将不胜感激任何帮助!

3 个答案:

答案 0 :(得分:0)

UINavigationController reference检查财产电话:

@property(nonatomic, copy) NSArray *viewControllers
  

<强>讨论
  根视图控制器位于数组中的索引0处,后视图控制器位于索引n-2处,顶部控制器位于索引n-1处,其中n是数组中的项目数。

这可以为你提供rootViewController的句柄 这对你来说是一个很好的解决方案吗? 还有其他方法。但是根据你的评论,我认为这可能是匹配。


[[self.navigationController viewControllers] objectAtIndex:0].someProerty;

答案 1 :(得分:0)

另一种方法可能是使用NSNotifications。让RootViewController重新注册为ThirdViewController发布的通知的观察者。然后在ThirdViewController发布的通知的userInfo字典中,将someInt作为NSNumber传递。

这提供了足够的抽象,因为你的ThirdViewController不需要知道什么类型的对象需要信息,它只是发布它所以感兴趣的观察者知道它。

祝你好运

答案 2 :(得分:0)

您永远不会在您发布的代码中向ThirdViewController的委托成员分配任何内容。此外,代表通常应该分配,而不是保留。