【发布时间】:2017-10-26 16:08:49
【问题描述】:
我正在尝试在我的项目中传递导航栏后退按钮上的数据,请帮助我。
【问题讨论】:
标签: ios objective-c
我正在尝试在我的项目中传递导航栏后退按钮上的数据,请帮助我。
【问题讨论】:
标签: ios objective-c
您可以在 viewDidDisapear 中检测何时按下后退按钮:
-(void)viewDidDisappear:(BOOL)animated{
if (self.isMovingFromParentViewController) {
//moving back
//pass to viewCollection delegate and update UI
[self.delegate passBackSavedData:self.dataModel];
}
}
【讨论】:
我有两个视图控制器。我正在使用自定义委托将数据从第二个视图控制器传递到第一个视图控制器。
这里我使用了自定义委托和 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中调用Custom Delegate时
- (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)sender;
{
//在这里设置委托
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.
}
【讨论】: