如何通过导航控制器

时间:2017-10-26 07:39:56

标签: ios objective-c

我正在尝试传递项目导航栏后退按钮上的数据,帮帮我。

2 个答案:

答案 0 :(得分:0)

我有两个View Controllers。我使用Custom Delegate将数据从Second View Controller传递到First View Controller。

这里我使用了Custom Delegate和NSNotification Center方法。

自定义代表

  

首先,我们可以使用自定义协议代理

     

@class SecondViewController;

     

@protocol SecondViewControllerDelegate <NSObject>

     

- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text;

     

@end

     

然后我们需要在这里分配代表。

     

@property (nonatomic, assign)id<SecondViewControllerDelegate> delegate;

     

非常重要的是我们必须在中设置自定义委托方法   第二个视图控制器。因为我们将数据发送到第一个视图   控制器一旦我们点击第二个视图控制器中的完成按钮。

     

[self.delegate secondViewController:self didEnterText:self.nameTextField.text];

     

最后,我们必须在First View中调用自定义委托方法   控制器,我们获取数据并将数据分配给label.Now   您可以使用自定义委托查看传递的数据。

     

#import "SecondViewController.h"

     

@interface ViewController : UIViewController<SecondViewControllerDelegate>

当您需要在First View Controller.m中调用自定义委托时

  

- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text { self.labelName.text = text; //Getting the data and assign the data to label here. }

但首先,当您导航到Second View Controller时,您需要执行以下操作

  

- (IBAction为)gotoNextView:(ID)发送者;

     

{

     

//在此处设置代理

     

secondViewController.delegate = self;

     

..... //导航代码

     

}

SecondViewController.h

#import <UIKit/UIKit.h>
@class SecondViewController;
@protocol SecondViewControllerDelegate <NSObject>
- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, assign)id<SecondViewControllerDelegate> delegate;  
@property (nonatomic, strong) IBOutlet UITextField *nameTextField;//It must connect as outlet connection
- (IBAction)doneButtonTapped:(id)sender;
@end

SecondViewController.m

#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
    // Custom initialization
   }
   return self;
}

- (void)viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view.
}

//Either use NSNotification or Delegate
- (IBAction)doneButtonTapped:(id)sender;
{
          //Use Notification
     [[NSNotificationCenter defaultCenter] postNotificationName:@"passingDataFromSecondViewToFirstView" object:self.nameTextField.text];
          //OR Custom Delegate
     [self.delegate secondViewController:self didEnterText:self.nameTextField.text];
     [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
}

然后

ViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface ViewController : UIViewController<SecondViewControllerDelegate>
@property (nonatomic, strong) IBOutlet UILabel *labelName; //You must connect the label with outlet connection
- (IBAction)gotoNextView:(id)sender;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
   [super viewDidLoad];
   //addObserver here...
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFromPreviousViewControllerNotificationReceived:) name:@"passingDataFromSecondViewToFirstView" object:nil];
   // Do any additional setup after loading the view, typically from a nib.
}

//addObserver Method here....
- (void)textFromPreviousViewControllerNotificationReceived:(NSNotification *)notification
{
   // set text to label...
   NSString *string = [notification object];
   self.labelName.text = string;
}
- (IBAction)gotoNextView:(id)sender;
{
   //If you use storyboard
   SecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
   //OR  If you use XIB
   SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
   secondViewController.delegate = self;
   [self.navigationController pushViewController:secondViewController animated:YES];
}

//Calling custom delegate method
- (void)secondViewController:(SecondViewController *)secondViewController didEnterText:(NSString *)text
{
   self.labelName.text = text; //Getting the data and assign the data to label here.
}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
 }

答案 1 :(得分:0)

您可以在viewDidDisapear:

中检测何时按下后退按钮
-(void)viewDidDisappear:(BOOL)animated{
     if (self.isMovingFromParentViewController) {
        //moving back
        //pass to viewCollection delegate and update UI
        [self.delegate passBackSavedData:self.dataModel];
     }
}
相关问题