ios 使用委托将数据传回导航堆栈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5244830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a delegate to pass data back up the navigation stack
提问by Matt Price
I have been battling with passing data between two view controllers for a couple of days now and getting very confused. I'm new to Objective-C and finding some parts tricky to get my head round.
几天来,我一直在与在两个视图控制器之间传递数据作斗争,并且变得非常困惑。我是 Objective-C 的新手,发现有些部分很难让我头脑清醒。
I have a Navigation Controller, FirstView is a form and on this form I have a button which loads SecondView which contains a TableView for the user to select some options. I then want to pass the selection back to the FirstView controller and display the data etc...
我有一个导航控制器,FirstView 是一个表单,在这个表单上我有一个加载 SecondView 的按钮,其中包含一个 TableView,供用户选择一些选项。然后我想将选择传递回 FirstView 控制器并显示数据等...
I have read alot about this (stackoverflow, iphonedevsdk, CS 193P Resources) and the options i've seen are,
我已经阅读了很多关于这个(stackoverflow、iphonedevsdk、CS 193P 资源)和我看到的选项,
1) ivar in app delegate (dirty and not recommended) 2) create a singleton 3) create a data model class 4) Use protocols and delegates (recommended by apple)
1)在应用程序委托的ivar(脏和不推荐)2)创建一个单3)创建数据模型类4)使用的协议和代表(由苹果推荐)
I want to do things right and want to use option 4 - Delegates in my program
我想把事情做对,并想在我的程序中使用选项 4 - 委托
Problem is, I don't understand delegates and how to setup and implement them.
问题是,我不了解委托以及如何设置和实现它们。
Could anyone provide a basic example on how to setup and pass an NSArray using the delegate and 2 view controllers.
任何人都可以提供有关如何使用委托和 2 个视图控制器设置和传递 NSArray 的基本示例。
Thanks in advance Matt
提前致谢马特
回答by Samyukt Shah
Delegation is the correct pattern to be used in this case, but your description doesn't look much like delegation as it is using a global variable. Perhaps you're storing global variables in your App Delegate which you should always try to avoid.
委托是在这种情况下使用的正确模式,但您的描述看起来不像委托,因为它使用全局变量。也许您将全局变量存储在您的 App Delegate 中,您应该始终尽量避免这种情况。
Here's a rough outline of what the code should look like:
以下是代码外观的粗略概述:
SecondViewController.h:
SecondViewController.h:
@protocol SecondViewControllerDelegate;
@interface SecondViewController;
SecondViewController : UIViewController
{
id<SecondViewControllerDelegate> delegate;
NSArray* someArray;
}
@property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;
@property (nonatomic, retain) NSArray* someArray;
@end
@protocol SecondViewControllerDelegate
- (void)secondViewControllerDidFinish:(SecondViewController*)secondViewController;
@end
SecondViewController.m:
SecondViewController.m:
@implementation SecondViewController
@synthesize delegate;
@synthesize someArray;
- (void)dealloc
{
[someArray release];
[super dealloc];
}
- (void)someMethodCalledWhenUserIsDone
{
[delegate secondViewControllerDidFinish:self];
}
FirstViewController.h:
FirstViewController.h:
#import SecondViewController
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>
{
...
}
@end
FirstViewController.m:
FirstViewController.m:
@implementation FirstViewController
- (void)secondViewControllerDidFinish:(SecondViewController*)secondViewController
{
NSArray* someArray = secondViewController.someArray
// Do something with the array
}
@end
回答by Eimantas
Off top of my head. You can replace _returnedProperty
with your custom object and in setReturnedProperty
method do all the magic before actually assigning the checked value from the table.
在我的头顶上。您可以替换_returnedProperty
为您的自定义对象,并在setReturnedProperty
实际从表中分配检查值之前在方法中执行所有魔术。
@interface FormController : UIViewController {
NSString *_returnedProperty;
}
@property (nonatomic, retain) NSString *returnedProperty;
@end
@implementation FormController
- (void)showChoices {
TableController *tv = [[TableController alloc] initWithDelegate:self];
[self.navigationController pushViewController:tv animated:YES];
[tv release];
}
- (void)setReturnedProperty:(NSString *)string {
NSLog(@"Setting property as a delegate");
[_returnedProperty release];
_returnedProperty = [string retain];
}
@synthesize returnedProperty=_returnedProperty;
@end
@interface TableController : UITableViewController {
id _delegate
}
@end
@implementation TableController
- (id)initWithDelegate:(id)delegate {
self = [super initWithStyle:UITableViewGroupedStyle];
if (!self) return nil;
_delegate = delegate;
return self;
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// do the data retrieval stuff
NSString *returnedProperty = @"foo";
[_delegate setReturnedProperty:returnableProperty];
}
@end
回答by Sudheesh
You can use storyboard its quite easy. Use this in the implementation of SecondViewController
and create a property in VIewController.h
(The first view's controller) named dataFromSecondView
你可以很容易地使用故事板。在实现中使用它SecondViewController
并在VIewController.h
(第一个视图的控制器)中创建一个名为 dataFromSecondView 的属性
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *textvalue = self.SecondViewText.text;
ViewController *destination = segue.destinationViewController;
destination.dataFromSecondView = textvalue;
}