我对iOS和Objective-C以及整个MVC范式都是新手,我一直坚持以下几点:
我有一个充当数据输入表单的视图,我想让用户选择多个产品。产品列在另一个具有UITableViewController的视图中,我已启用多个选项。
如何将数据从一个视图传输到另一个视图?我会将UITableView上的选择保存在一个数组中,但如何将其传递回上一个数据输入表单视图,以便在提交表单时将其与其他数据一起保存到核心数据?
我在网上冲浪,看到一些人在应用程序委托中声明了一个数组。我读了一些关于单态的东西,但我不明白这些是什么,我读了关于创建数据模型的东西。
正确的执行方式是什么?我将如何执行?
我找到了最简单、最优雅的版本,带有传递块。让我们将等待返回数据的视图控制器命名为“A”,将返回的视图控制器称为“B”。在本例中,我们希望获得两个值:第一个是Type1,第二个是Type2。
假设我们使用Storyboard,第一个控制器设置回调块,例如在segue准备期间:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isKindOfClass:[BViewController class]])
{
BViewController *viewController = segue.destinationViewController;
viewController.callback = ^(Type1 *value1, Type2 *value2) {
// optionally, close B
//[self.navigationController popViewControllerAnimated:YES];
// let's do some action after with returned values
action1(value1);
action2(value2);
};
}
}
和“B”视图控制器应声明回调属性BViewController.h:
// it is important to use "copy"
@property (copy) void(^callback)(Type1 *value1, Type2 *value2);
在实现文件BViewController.m中,当我们有了所需的值以返回回调时,应该调用:
if (self.callback)
self.callback(value1, value2);
需要记住的一点是,使用块通常需要管理强引用和弱引用,如这里所述
您可以创建一个从源视图控制器到目标视图控制器的推送段,并提供如下标识符名称。
您必须像这样执行didselectRowAt的segue。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "segue", sender: self)
}
您可以从下面的函数传递选定项的数组。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let index = CategorytableView.indexPathForSelectedRow
let indexNumber = index?.row
print(indexNumber!)
let VC = segue.destination as! AddTransactionVC
VC.val = CategoryData[indexNumber!] . // Here you can pass the entire array instead of an array element.
}
您必须检查目标viewcontroller的viewdidload中的值,然后将其存储到数据库中。
override func viewDidLoad{
if val != ""{
btnSelectCategory.setTitle(val, for: .normal)
}
}
使用通知中心
对于Swift 3
let imageDataDict:[String: UIImage] = ["image": image]
// Post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// Handle notification
func showSpinningWheel(_ notification: NSNotification) {
print(notification.userInfo ?? "")
if let dict = notification.userInfo as NSDictionary? {
if let id = dict["image"] as? UIImage {
// Do something with your image
}
}
}
对于Swift 4
let imageDataDict:[String: UIImage] = ["image": image]
// Post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict)
// `default` is now a property, not a method call
// Register to receive notification in your class
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil)
// Handle notification
@objc func showSpinningWheel(_ notification: NSNotification) {
print(notification.userInfo ?? "")
if let dict = notification.userInfo as NSDictionary? {
if let id = dict["image"] as? UIImage {
// Do something with your image
}
}
}
将数据从ViewController 2(目标)传递回ViewController 1(源)是更有趣的事情。假设你使用故事板,我发现了以下所有方法:
代表通知用户默认值辛格尔顿
这些已经在这里讨论过了。
我发现有更多的方法:
使用块回调:
在VC1的prepareForSegue方法中使用它
NextViewController *destinationVC = (NextViewController *) segue.destinationViewController;
[destinationVC setDidFinishUsingBlockCallback:^(NextViewController *destinationVC)
{
self.blockLabel.text = destination.blockTextField.text;
}];
使用故事板展开(退出)
在VC1中实现一个带有UIStoryboardSegue参数的方法,如下所示:
-(IBAction)UnWindDone:(UIStoryboardSegue *)segue { }
在故事板中,将“返回”按钮挂在vc的绿色退出按钮(展开)上。现在您有了一个“返回”的segue,因此您可以使用VC2的prepareForSegue中的destinationViewController属性在返回之前更改VC1的任何属性。
使用故事板的另一种选择Unwind(退出)-您可以使用在VC1中编写的方法-(IBAction)UnWindDone:(UIStoryboardSegue*)segue{NextViewController*NextViewController=segue.sourceViewController;self.unindLabel.text=nextViewController.undPropertyPass;}
在VC1的prepareForSegue中,您可以更改您想要共享的任何属性。
在两个展开选项中,您都可以设置按钮的标记属性,并在prepareForSegue中进行检查。
如果要将数据从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);
}