我对iOS和Objective-C以及整个MVC范式都是新手,我一直坚持以下几点:
我有一个充当数据输入表单的视图,我想让用户选择多个产品。产品列在另一个具有UITableViewController的视图中,我已启用多个选项。
如何将数据从一个视图传输到另一个视图?我会将UITableView上的选择保存在一个数组中,但如何将其传递回上一个数据输入表单视图,以便在提交表单时将其与其他数据一起保存到核心数据?
我在网上冲浪,看到一些人在应用程序委托中声明了一个数组。我读了一些关于单态的东西,但我不明白这些是什么,我读了关于创建数据模型的东西。
正确的执行方式是什么?我将如何执行?
如果要将数据从ViewControlerOne传递到ViewControlerTwo,请尝试以下操作。。。
在ViewControlerOne.h中执行以下操作:
@property (nonatomic, strong) NSString *str1;
在ViewControllerTwo.h中执行以下操作:
@property (nonatomic, strong) NSString *str2;
在ViewControllerTwo.m中合成str2:
@interface ViewControllerTwo ()
@end
@implementation ViewControllerTwo
@synthesize str2;
在ViewControlerOne.m中执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
// Data or string you wants to pass in ViewControllerTwo...
self.str1 = @"hello world";
}
O按钮单击事件,请执行以下操作:
-(IBAction)ButtonClicked
{
// Navigation on buttons click event from ViewControlerOne to ViewControlerTwo with transferring data or string..
ViewControllerTwo *objViewTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerTwo"];
obj.str2 = str1;
[self.navigationController pushViewController: objViewTwo animated:YES];
}
在ViewControllerTwo.m中执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@", str2);
}
有了Swift的倾向,并想要一个简单的例子,如果你使用segue来绕过,这里是我传递数据的go to方法。
它与上述类似,但没有按钮、标签等。只需将数据从一个视图传递到下一个视图。
设置情节提要
有三个部分。
发件人赛格牌手表接收者
这是一个非常简单的视图布局,中间有一个segue。
这是发件人的设置
这是接收器的设置。
最后,segue的设置。
视图控制器
我们保持这个简单,所以没有按钮,没有动作。我们只是在应用程序加载时将数据从发送方移动到接收方,然后将传输的值输出到控制台。
此页面获取初始加载的值并将其传递。
import UIKit
class ViewControllerSender: UIViewController {
// THE STUFF - put some information into a variable
let favoriteMovie = "Ghost Busters"
override func viewDidAppear(animated: Bool) {
// PASS IDENTIFIER - go to the receiving view controller.
self.performSegueWithIdentifier("goToReciever", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// GET REFERENCE - ...to the receiver view.
var viewControllerReceiver = segue.destinationViewController as? ViewControllerReceiver
// PASS STUFF - pass the variable along to the target.
viewControllerReceiver!.yourFavMovie = self.favoriteMovie
}
}
此页面仅在加载时将变量的值发送到控制台。至此,我们最喜欢的电影应该在这个变量中。
import UIKit
class ViewControllerReceiver: UIViewController {
// Basic empty variable waiting for you to pass in your fantastic favorite movie.
var yourFavMovie = ""
override func viewDidLoad() {
super.viewDidLoad()
// And now we can view it in the console.
println("The Movie is \(self.yourFavMovie)")
}
}
如果你想使用segue,而你的页面不在导航控制器下,那么这就是解决问题的方法。
一旦运行,它应该自动切换到接收方视图,并将值从发送方传递给接收方,在控制台中显示该值。
在Swift中传递数据有很多解决方案。
向前传递数据
我最喜欢的两种转发数据的方式是依赖注入(DI)和属性观察者
依赖注入
class CustomView : UIView {
init(_ with model : Model) {
// Do what you want with data
}
}
财产观察员
class CustomView : UIView {
var model : Model? {
didSet {
// Do what you want with data after assign data to model
}
willSet {
// Do what you want with data before assign data to model
}
}
}
向后传递数据
还喜欢将数据传递到上一个VC/视图的方法:
协议和代表
protocol CustomViewDelegate : class {
func addItemViewController(_ with data: Model?)
}
weak var delegate : CustomViewDelegate?
class AnotherCustomView: UIView {
let customView = AnotherCustomView()
init() {
customView.delegate = self
}
}
extention AnotherCustomView : CustomViewDelegate {
func addItemViewController(_ with data: Model?) {
// Do what you want with data
}
}
关闭
class AnotherCustomView : UIView {
init(addItem: @escaping (_ value : Model?) -> ()) {
// Do what you want with data
}
}
class CustomView : UIView {
init() {
let customView = AnotherCustomView { [weak self] model in
// Do what you want with data
}
}
}