ios 在目标视图控制器中获取 segue 的发送者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14964487/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:22:07  来源:igfitidea点击:

get sender of segue in destination view controller

iosobjective-cstoryboardsegue

提问by Thomas Leduc

I have a VC named Dashboard (D) which can open a VC named Login (L) and a VC named Register (R). Login can open VC Register too.

我有一个名为 Dashboard (D) 的 VC,它可以打开一个名为 Login (L) 的 VC 和一个名为 Register (R) 的 VC。登录也可以打开VC注册。

I try to use storyboard as often as possible, so I have created with it three Segues, D to L, D to R, L to R

我尽量经常使用故事板,所以我用它创建了三个 Segue,D 到 L,D 到 R,L 到 R

So, in case of D -> L -> R and in case of D -> R, when I close R, I have to close L if it necessary and inform D which he can begin to load the user infos (launch function in nutshell).

因此,在 D -> L -> R 和 D -> R 的情况下,当我关闭 R 时,我必须在必要时关闭 L 并通知 D 他可以开始加载用户信息(启动功能在简而言之)。

So, I would like get the sender of Segue in destination vc, knowing that I put it in sender entrie of performSegueWithIdentifier like that :

所以,我想在目的地 vc 中获取 Segue 的发送者,知道我把它放在 performSegueWithIdentifier 的发送者条目中:

[self performSegueWithIdentifier:@"SegueToFbRegister" sender:self];

采纳答案by Taum

I'd do this by having R send a notification when the registration/login is done, and having D listen to it then pop everything and load your data.

我会通过在注册/登录完成时让 R 发送通知来做到这一点,让 D 听它然后弹出所有内容并加载您的数据。

If however you insist on getting a reference to the sender, you can add this property on your destination VC and set it in the source VC's prepareForSegue:sender:

但是,如果您坚持要获得对发件人的引用,则可以在目标 VC 上添加此属性并将其设置在源 VC 的 prepareForSegue:sender:

回答by johnrechd

This sounds like a great place to use Delegates. In your RegisterViewController.h define a protocol like this

这听起来像是使用委托的好地方。在你的 RegisterViewController.h 中定义一个这样的协议

@protocol RegisterViewDelegate <NSObject>
- (void)tellRegisterDelegateSomething:(NSObject*)something;
@end

Then on your class keep a pointer to your delegate

然后在你的课堂上保留一个指向你的代表的指针

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

Now tell the presenting view controllers that they implement the new protocol you just created. This is done in the .h files of the other viewcontrollers that present this view.

现在告诉呈现视图控制器他们实现了你刚刚创建的新协议。这是在呈现此视图的其他视图控制器的 .h 文件中完成的。

In LoginViewController.h

在 LoginViewController.h

@interface LoginViewController : UIViewController <RegisterViewDelegate>
@end

In DashboardViewController.h

在 DashboardViewController.h 中

@interface DashboardViewController : UIViewController <RegisterViewDelegate>
@end

In the .m files of the above classes, implement the protocol's method

在上述类的.m文件中,实现协议的方法

- (void)tellRegisterDelegateSomething:(NSObject*)something
{
}

Now you need to assign the delegate when you perform your segue from either presenting view controller like this.

现在,当您从这样的呈现视图控制器执行转场时,您需要分配委托。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"SegueToFbRegister"])
    {
        RegisterViewController* destination = [segue destinationViewController];
        destination.delegate = self;
    }
}

Now you can call the presenting view controller (delegate) and have it do something with any information you need to send back like this (this would be called in your RegisterViewController.m).

现在您可以调用呈现视图控制器(委托)并让它处理您需要像这样发回的任何信息(这将在您的 RegisterViewController.m 中调用)。

if ([self.delegate respondsToSelector:@selector(tellRegisterDelegateSomething:)])
{
    // Tell the delegate something.
    [self.delegate tellRegisterDelegateSomething:something];
}

The instance where you need to pass back through two controller you follow the same basic pattern.

您需要通过两个控制器传递回的实例,您遵循相同的基本模式。

@protocol LoginViewDelegate <NSObject>
- (void)tellLoginDelegateSomething:(NSObject*)something;
@end

Then on your class keep a pointer to your delegate

然后在你的课堂上保留一个指向你的代表的指针

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

Now tell the Dashboard view controller that it implements the protocol. This is done in the .h files of the Dashboard viewcontrollers that present this view.

现在告诉仪表板视图控制器它实现了协议。这是在显示此视图的仪表板视图控制器的 .h 文件中完成的。

In DashboardViewController.h

在 DashboardViewController.h 中

@interface DashboardViewController : UIViewController <RegisterViewDelegate, LoginViewDelegate>
@end

In the .m files of the DashboardViewController implement the protocol's method

在 DashboardViewController 的 .m 文件中实现协议的方法

Follow the above pattern of setting the delegate on the viewcontroller when you perform the segue. Now when the delegate method is called in the LoginViewController, you call the delegate in the DashboardViewController as well.

执行 segue 时,请遵循上述在视图控制器上设置委托的模式。现在,当在 LoginViewController 中调用委托方法时,您也在 DashboardViewController 中调用委托。

in LoginViewController.m

在 LoginViewController.m 中

- (void)tellRegisterDelegateSomething:(NSObject*)something
{
    if ([self.delegate respondsToSelector:@selector(tellLoginDelegateSomething:)])
    {
        // Tell the delegate something.
        [self.delegate tellLoginDelegateSomething:something];
    }
}

Now you are all connected so you can pass data back through both controllers (or just one) and do something with it. You will know which scenario you are in because different delegate methods will be called in the DashboardViewController based on which viewcontroller was visible.

现在您都已连接,因此您可以通过两个控制器(或仅一个)将数据传回并使用它做一些事情。您将知道您处于哪种场景,因为将根据哪个视图控制器可见在 DashboardViewController 中调用不同的委托方法。

Hope this helps.

希望这可以帮助。

回答by Abdurrahman Mubeen Ali

Another way would be to use an unwind segue.

另一种方法是使用 unwind segue。

Place the following code in you Dashboard(D) view controller.

将以下代码放在您的Dashboard( D) 视图控制器中。

@IBAction func loadUserInfoAfterRegistration(segue: UIStoryboardSegue) {

}

In Interface Builder, do the following steps for the Register(R) view controller:

在 Interface Builder 中,对Register( R) 视图控制器执行以下步骤:

  1. Select the button that will be pressed on the completion of registration.
  2. Ctrl+ dragto the exit symbol on top of the view.
  3. Select loadUserInfoAfterRegistrationWithSegue:from the list displayed.
  1. 选择注册完成后将按下的按钮。
  2. Ctrl+拖动到视图顶部的退出符号。
  3. loadUserInfoAfterRegistrationWithSegue:从显示的列表中选择。

Using this approach, the Register(R) view controller will always navigate to the Dashboard(D) view controller, regardless of what is between them. The view controllers between them will not have to be touched. The loading of the user data in the Dashboard(D) view controller can also be customized in the method declared above.

使用这种方法,注册( R) 视图控制器将始终导航到仪表板( D) 视图控制器,无论它们之间有什么。它们之间的视图控制器将不必被触摸。在Dashboard( D) 视图控制器中加载用户数据也可以在上面声明的方法中自定义。

回答by kaar3k

Create a delegate for Rand make Dand Lto implement the delegate methods.Use prepareForSegue:senderto assign the delegate of R.When you finish task in Ruse your delegate to perform the rquired action.

R创建一个委托并使DL实现委托方法。prepareForSegue:sender用于分配R的委托。当您在R 中完成任务时,使用您的委托执行所需的操作。