【问题标题】:SWRevealViewController FrontViewController not loading, stuck as Black, other views fineSWRevealViewController FrontViewController 未加载,卡为黑色,其他视图正常
【发布时间】:2015-02-03 02:19:35
【问题描述】:

我正在尝试将我们的应用程序转换为使用 SWRevealViewController,以便在应用程序的每一侧为我们提供一个侧边栏。但是,尽管遵循其中一个示例应用程序中的代码,但我收到了一个错误,即前视图的 ViewController 无法正常工作。 viewDidLoad 被调用,但一切仍然是黑色的。

有趣的是,如果在我的 viewDidLoad 中,我将视图的背景颜色设置为红色,这会反映出来。但是原始故事板中的界面生成器中的东西不是。

我在 AppDelegate 中使用的代码是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window = window;

    MainViewController *frontViewController = [[MainViewController alloc] init];
    RearViewController *rearViewController = [[RearViewController alloc] init];

    UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
    UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

    SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
    revealController.delegate = self;

    RightViewController *rightViewController = rightViewController = [[RightViewController alloc] init];
    rightViewController.view.backgroundColor = [UIColor greenColor];

    revealController.rightViewController = rightViewController;

    //revealController.bounceBackOnOverdraw=NO;
    //revealController.stableDragOnOverdraw=YES;

    self.viewController = revealController;

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark - SWRevealViewDelegate

- (id <UIViewControllerAnimatedTransitioning>)revealController:(SWRevealViewController *)revealController animationControllerForOperation:(SWRevealControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if ( operation != SWRevealControllerOperationReplaceRightController )
        return nil;

    if ( [toVC isKindOfClass:[RightViewController class]] )
    {
        if ( [(RightViewController*)toVC wantsCustomAnimation] )
        {
            id<UIViewControllerAnimatedTransitioning> animationController = [[CustomAnimationController alloc] init];
            return animationController;
        }
    }

    return nil;
}

这是仅用于 MainViewController 的 Main.Storyboard:

当应用加载时,视图只是黑色的。但是我可以从左右边缘拖动并按预期查看侧边栏。所以只有 FrontView 会变黑。我将一个 NSLog() 插入到 ViewDidLoad 中,它出现在控制台中,就像 -(void)loadView{} 中的一个一样,它显示视图正在加载。

如果我将[self.view setBackgroundColor:[UIColor redColor]] 放入viewDidLoad 中,这将生效,这意味着它已链接到视图,但只是情节提要内的视图没有出现。这很奇怪,因为 Storyboard 还包含一个 NavigationController,它确实出现了(我认为 - 除非该导航控制器来自其他地方 - 我很确定它不是)。

有什么可能导致这种情况的想法吗?

【问题讨论】:

    标签: ios objective-c storyboard swrevealviewcontroller


    【解决方案1】:

    我也遇到了同样的事情。我在查看示例代码时修复了它。

    1. 在您的 Storyboard 中添加一个 UIViewController
    2. 选择它并在 Identity Inspector 中使用 'SWRevealViewController' 作为类
    3. 将 UITableViewController 添加到您的故事板
    4. 现在选择您之前添加的 ViewControler 并右键单击并在您的 TableViewController 中绘制一条线
    5. 选择“显示视图控制器集控制器”
    6. 点击新添加的 Segue 并在 Attribute Inspector 中将标识符更改为 'sw_rear'
    7. 将任何自定义 ViewController(例如“MyViewController”)添加到故事板
    8. 选择它,然后转到菜单->编辑器->嵌入->导航控制器
    9. 现在应该会出现一个新的导航控制器
    10. 再次右键单击并从第一个 ViewController 到您的新 NavigationController 画一条线
    11. 再次选择“显示视图控制器集控制器”
    12. 现在将此新 Segue 的标识符设置为“sw_front”

    现在您已经完成了基本设置,并且在运行您的应用程序时,您应该会看到您的自定义 ViewController。现在必须添加菜单的按钮。

    在您的 ViewControllers .m 中添加以下内容:

    @interface MyViewController ()
    @property (nonatomic) IBOutlet UIBarButtonItem* revealButtonItem;
    @end
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self customSetup];
    }
    
    - (void)customSetup
    {
        SWRevealViewController *revealViewController = self.revealViewController;
        if ( revealViewController )
        {
            [self.revealButtonItem setTarget: self.revealViewController];
            [self.revealButtonItem setAction: @selector( revealToggle: )];
            [self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
        }
    }
    

    现在再次切换到故事板。将 UIBarButtonItem 添加到 MyViewController。再次右键单击从 MyViewController 到 NavigationBar 中的这个新项目绘制一条线。选择“显示按钮项目”。

    就是这样!对要添加的每个 ViewController 重复最后的步骤。您只需将它们从 TableView 中通过右键单击绘图连接到您添加的每个 ViewController 的新添加 NavigationController。要推送 ViewControllers,只需选择“显示视图控制器推送控制器”。

    希望能有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多