【问题标题】:SWReveal - change frontviewcontroller on application startSWReveal - 在应用程序启动时更改 frontviewcontroller
【发布时间】:2015-05-22 16:53:23
【问题描述】:

我正在使用SWRevealViewController 在我的应用程序中实现侧边菜单。在情节提要中,我设置了sw_front segue。这个 segue 在每次启动应用程序时加载 MainView。现在我想给用户更改第一个显示的视图控制器的可能性。

我该怎么做?我知道有一个属性setFrontViewController:,但是我应该在哪里使用它在应用程序启动期间加载前端?

我不能在我在 Storyboard 中设置的 frontViewControllerviewDidLoad 中使用它。这不可能是一个解决方案,所以我需要在每个视图控制器中执行它来抓取并检查“应该首先加载另一个视图控制器或frontViewController

【问题讨论】:

  • 如果您发现我的解决方案正确,请将答案标记为正确。
  • @jogshardik 您的解决方案也可以。我找到了一个简单的解决方案,它也是更新稳定的。你的回答也是正确的,我会标记的。但也请检查我的解决方案,我会将其添加为其他人的答案。
  • 好的,等我有时间,我会实施并检查您的解决方案。

标签: ios uiviewcontroller storyboard segue swrevealviewcontroller


【解决方案1】:

@jogshardik 对我的问题有正确的答案,但我也找到了另一种解决方案。

在您的故事板中,您将自定义 segue“sw_front”与您的视图控制器连接起来。此视图控制器在您的第一个前视图控制器的每次初始启动时。所以如果你需要设置另一个前视图控制器,你只需要签入这个控制器。

我在这个“sw_front”视图控制器中设置了一个通知,还在你的侧边菜单视图控制器中添加了通知处理程序。

如果您想更改视图,只需触发通知并让通知处理程序完成其余的工作(设置您的 setFrontViewController: 属性)。

因此,如果您需要关注新版本,这也有效并且更新稳定。

如果这些信息对您来说还不够,请给我一个提示,我会在有时间时发布示例代码或项目。

【讨论】:

  • “sw_front”视图控制器的哪个方法发送这样的通知?它不能是 initWithCoder。对吗?
【解决方案2】:

我已经了解您有什么问题以及您想要做什么,我也有同样的事情要做,所以我所做的就是在我的代码中提到的 swrevealviewcontroller 中创建了自定义 segue。 并且修改了perform和loadstoryboardcontroller这两个方法,在我的代码中也可以看到。 它肯定也适合你。

祝你编码愉快。

我的代码如下:

我已经完成了,您需要更改的代码如下。

- (void)loadStoryboardControllers
{
if ( self.storyboard && _rearViewController == nil )
{
//Try each segue separately so it doesn't break prematurely if either Rear or Right views are not used.
@try
{
[self performSegueWithIdentifier:SWSegueRearIdentifier sender:nil];
}
@catch(NSException *exception) {}

@try
{
[self performSegueWithIdentifier:SWSegueFrontIdentifier sender:nil];
}
@catch(NSException *exception) {}

@try
{
[self performSegueWithIdentifier:SWSegueRightIdentifier sender:nil];
}
@catch(NSException *exception) {}
@try
{
[self performSegueWithIdentifier:SWSegueCustomIdentifier sender:nil];
}
@catch(NSException *exception) {}
}
}
#pragma mark - SWRevealViewControllerSegueSetController segue identifiers

NSString * const SWSegueRearIdentifier = @"sw_rear";
NSString * const SWSegueFrontIdentifier = @"sw_front";
NSString * const SWSegueRightIdentifier = @"sw_right";
NSString * const SWSegueCustomIdentifier = @"sw_custom";

#pragma mark - SWRevealViewControllerSegueSetController class

@implementation SWRevealViewControllerSegueSetController

- (void)perform
{
SWRevealControllerOperation operation = SWRevealControllerOperationNone;

NSString *identifier = self.identifier;
SWRevealViewController *rvc = self.sourceViewController;
UIViewController *dvc = self.destinationViewController;

if ( [identifier isEqualToString:SWSegueFrontIdentifier] )
operation = SWRevealControllerOperationReplaceFrontController;

else if ( [identifier isEqualToString:SWSegueRearIdentifier] )
         operation = SWRevealControllerOperationReplaceRearController;

else if ( [identifier isEqualToString:SWSegueRightIdentifier] )
operation = SWRevealControllerOperationReplaceRightController;

else if ( [identifier isEqualToString:SWSegueCustomIdentifier])//your conditional segue
operation = SWRevealControllerOperationReplaceFrontController;

if ( operation != SWRevealControllerOperationNone )
[rvc _performTransitionOperation:operation withViewController:dvc animated:NO];
}

【讨论】:

  • 如何在@"sw_front" 和自定义的之间切换。
  • 你必须把你的条件放在那里,正如我在代码中提到的那样
  • 对不起,我有一个问题。如何使用 SWSegueCustomIdentifier 和 sw_custom?有什么例子可以参考吗? github.com/John-Lluch/SWRevealViewController/issues/820
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多