ios - SWRevealViewController 不切换

标签 ios objective-c navigation swrevealviewcontroller

我正在尝试在不使用 Storyboard的情况下在项目中实现 SWRevealViewController。我认为这是可能的,但不幸的是我失败了。导航栏中的按钮不执行在他身上定义的“revealToggle” Action 。所以 NavigationTableViewController 永远不会显示。我不明白为什么......我搜索了几个小时来解决这个问题。任何人的帮助将不胜感激。

#import "ContentViewController.h"
#import "NavigationTableViewController.h"

@interface ContentViewController()<SWRevealViewControllerDelegate>

@end

@implementation ContentViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view from its nib.
//[self.navigationItem setHidesBackButton:YES animated:YES];

UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;

ContentViewController *frontViewController = self;
NavigationTableViewController *rearViewController = [[NavigationTableViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearViewController frontViewController:frontNavigationController];
revealController.delegate = self;

[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];


self.viewController = revealController;
self.window.rootViewController = self.viewController;

UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal-icon.png"]
                                                                     style:UIBarButtonItemStyleBordered target:revealController action:@selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = revealButtonItem;

}

最佳答案

我终于设法解决了我的问题。根据John Lluch examples ,我修改了我的代码。 这是不使用 Storyboard的解决方案:

AppDelegate.h

#import <UIKit/UIKit.h>
#import "SplashScreenController.h"
#import "SWRevealViewController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController;
@property (strong, nonatomic) SplashScreenController *viewController;
@property (strong, nonatomic) SWRevealViewController *revealController;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "ContentViewController.h"
#import "NavigationTableViewController.h"


@interface AppDelegate()<SWRevealViewControllerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    SplashScreenController *frontViewController = [[SplashScreenController alloc] initWithNibName:@"SplashScreenController" bundle:nil];
    NavigationTableViewController *rearViewController = [[NavigationTableViewController alloc] init];
    UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
    SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearViewController frontViewController:frontNavigationController];

    revealController.delegate = self;

    self.window.rootViewController = revealController;

    [self.window makeKeyAndVisible];

    return YES;

}

ContentViewController.h

#import <UIKit/UIKit.h>

@interface ContentViewController : UIViewController
@end

ContentViewController.m

#import "ContentViewController.h"
#import "SWRevealViewController.h"
#import "NavigationTableViewController.h"
#import <sqlite3.h>

@interface ContentViewController()<SWRevealViewControllerDelegate>

@end

@implementation ContentViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    SWRevealViewController *revealController = [self revealViewController];
    [revealController panGestureRecognizer];
    [revealController tapGestureRecognizer];

    UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal-icon.png"] style:UIBarButtonItemStyleBordered target:revealController action:@selector(revealToggle:)];

    self.navigationItem.leftBarButtonItem = revealButtonItem;

}

关于ios - SWRevealViewController 不切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27128686/

相关文章:

ios - Swift 无法通过委托(delegate)调用协议(protocol)方法

ios - Spritekit游戏在iPad模拟器上出现严重滞后

ios - 如何在tableview的底部插入自定义 View ?

objective-c - 分析autorelease调用class_createInstance返回的对象报错

c# - 终止应用程序时导致的 System.Runtime.InteropServices.COMException

c# - 使用MVVM在Silverlight页面之间导航

iphone - 重新加载数据时 tableView 会崩溃

objective-c - 可以悬垂 NSWindow 的自定义工具提示吗?

弹出键盘时 iOS 11 无限滚动

android - android应用程序中的模块间(库项目)通信