iOS中类似于Android的startActivityForResult/setResult的方法是什么?

3

我是一名学习iOS的安卓程序员。我有一个Swift视图1调用一个Objective C视图2。我想要从视图2返回一个字符串到视图1。

在安卓中,我们会简单地使用view1.startActivityForResult(View2.class)

iOS中该怎么做呢?


1
这与您之前的问题有何不同(您已接受答案)? - rmaddy
3个回答

6

ViewController2.h - Objective-C

@protocol ViewController2Delegate <NSObject>
- (void)sendStringBack:(NSString *)aString;
@end 

@interface ViewController2 : UIViewController
@property (nonatomic, weak) id<ViewController2Delegate> delegate;
@end

ViewController2.m - Objective-C

// When you want to send the string back and dismiss the view:
[self.delegate sendStringBack:theStringToSendBack];

ViewController1.swift - Swift

@objc class ViewController1: UIViewController, ViewController2Delegate {
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let viewContr = segue.destinationViewController as! ViewController2
        viewContr.delegate = self
    }

    func sendStringBack(aString: String) {
        let aVariable = aString
        // do something with the string

        // dismiss the view if you presented it modally
        self.dismissViewController(self, animated: true, completion: nil)

        // OR
        // dismiss the view if you presented it with show/push
        self.navigationController?.popViewControllerAnimated(true)
    }
}

1
你好,非常好的答案,但是你能否再详细解释一下吗?在ViewController1上是否需要采取任何操作?我需要创建sendStringBack方法吗? - Alan Faz
在ViewController1中,您必须声明遵循ViewController2Delegate(位于@objc类行中)。一旦编译器知道您遵循ViewController2Delegate,它将告诉您实现sendStringBack()。使用智能感知,sendStringBack应该自动完成。 - keithbhunter

1
在iOS中没有类似于startActivityForResult()的功能,您需要手动完成。我使用以下过程完成了此操作。 ViewControllerOne有一个表单,需要从viewControllerTwo获取数据。ViewControllerTwo有一个带有dataList的tableView。通过点击ViewControllerTwo的tableView上的数据,它将返回到ViewControllerOne,并携带被点击的表格数据。
  1. 创建一个用于存储数据的全局变量。 var data:String = ""
  2. 使用segue从ViewControllerOne打开ViewControllerTwo。
  3. 在具有tablewView的ViewControllerTwo中,在didSelectRowAt方法中将数据设置为全局变量并调用以下方法 self.navigationController?.popViewController(animated: true)。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { data = dataList[indexPath.row] self.navigationController?.popViewController(animated: true) }

  4. 它将返回到ViewControllerOne。现在您需要在viewWillAppear()或viewDidAppear()方法中捕获数据。例如:

    override func viewWillAppear(_ animated: Bool) { if data != ""{ // 在处理数据后,将数据值设为空。 data = "" } }

就是这样。祝编程愉快。它一定会正常工作的。

-1

使用UIViewController+CallBack来实现此目的。

只需设置父控制器中的结果对象,子控制器将覆盖协议方法并返回任何类型的对象给父控制器。

  • (id)viewUnloadWithResultObject

无需设置协议。

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"NewViewController"];
    [controller setResultBlock:^(id resultObject) {
        NSLog(@"New View Controller did pop and resulting object is: %@", resultObject);
    }];

    [self.navigationController pushViewController:controller animated:YES];

你也可以呈现视图控制器并获得相同的结果对象

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"NewViewController"];
    [controller setResultBlock:^(id resultObject) {
        NSLog(@"New View Controller did dismissed and resulting object is: %@", resultObject);
    }];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];

    [self.navigationController presentViewController:navController animated:YES completion:NULL];

你也可以拥有视图卸载回调方法,例如:

  • (void)viewWillUnloadCallBack
  • (void)viewDidUnloadCallBack

只需继承协议并在您的视图控制器中重写这些方法即可获得卸载回调。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接