由于_delegate为nil而未调用Delegate方法

时间:2015-06-16 15:25:32

标签: ios objective-c uiviewcontroller delegates

ViewController使用另一个视图控制器MainViewController作为委托。当按下[_delegate movePanelRight];中的导航项时,应该运行行ViewController。但是当我在NSLog movePanelRight中添加MainViewController.m时,movePanelRight显示AppDelegate.m永远不会运行。

我的导航栏,用于初始视图控制器,在MainViewController.m中创建。这也是为nil

完成委托代码的地方

我使用以下代码推断出委托是if (_delegate == nil) { NSLog(@"Ah, you fool, delegate is nil!"); } 。这可能是该方法未被调用的原因。

[_delegate movePanelRight];

希望这可以帮助您解决方法#import <UIKit/UIKit.h> @class MainViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) MainViewController *viewController; @end 未被调用的原因。

AppDelegate.h

#import "AppDelegate.h"
#import "MainViewController.h"
#import "SlideMenuController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
    NSShadow* shadow = [NSShadow new];
    shadow.shadowOffset = CGSizeMake(1.0f, 1.0f);
    shadow.shadowColor = [UIColor clearColor];
    [[UINavigationBar appearance] setTitleTextAttributes: @{
                                                            NSForegroundColorAttributeName: [UIColor whiteColor],
                                                            NSFontAttributeName: [UIFont fontWithName:@"Aliquam" size:20.0f],
                                                            NSShadowAttributeName: shadow
                                                            }];

    UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main"
                                                  bundle:nil];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [sb instantiateViewControllerWithIdentifier:@"MainViewControllerSB"];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];


    NSLog(@"Yeah, it's done!");
    return YES;
}

AppDelegate.m

#import <UIKit/UIKit.h>

@protocol ViewControllerDelegate <NSObject>

@optional
- (void)movePanelLeft;
- (void)movePanelRight;

@required
- (void)movePanelToOriginalPosition;

@end

#import "SlideMenuController.h"

@interface ViewController : UIViewController <SlideMenuControllerDelegate>

@property (nonatomic, assign) id<ViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UINavigationItem *navigationItemMain;
@property (nonatomic, strong) UIBarButtonItem *menuButton;
@property (strong, nonatomic) UIWindow *window;

@end

ViewController.h

#import "ViewController.h"
#import "BasicSearchController.h"

@interface ViewController ()

@end

ViewController *viewController;

// Interface variables
UIImageView *backgroundView;
UIImageView *backgroundDM;
UIImageView *underBarGradient;
UIBarButtonItem *searchButton;

// Screen dimensions variables
double screenWidth;
double screenHeight;
UIScreen *screen;
CGRect screenRect;

@implementation ViewController

@synthesize navigationItemMain;
@synthesize menuButton;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    viewController = [[ViewController alloc] init];
    menuButton.tag = 1;
    [self createInterface];
}

- (void) createInterface {
    screenRect = [[UIScreen mainScreen] bounds];
    screenWidth = screenRect.size.width;
    screenHeight = screenRect.size.height;

    UIImage *navBackgroundImage = [UIImage imageNamed:@"bar.png"];
    [[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

    backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background1.png"]];
    [backgroundView setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    [backgroundView setContentMode:UIViewContentModeScaleToFill];
    [[self view] addSubview:backgroundView];
    [[self view] bringSubviewToFront:backgroundView];

    backgroundDM = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dm-for-background-final.png"]];
    [backgroundDM setFrame:CGRectMake(screenWidth/4, (screenHeight/2)-(screenWidth/4), screenWidth/2, screenWidth/2)];
    [backgroundDM setContentMode:UIViewContentModeScaleAspectFit];
    [[self view] addSubview:backgroundDM];
    [[self view] bringSubviewToFront:backgroundDM];

    underBarGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient-under-bar.png"]];
    [underBarGradient setFrame:CGRectMake(0, 64, screenWidth, 3)];
    [underBarGradient setContentMode:UIViewContentModeScaleToFill];
    [underBarGradient setAlpha:0.5f];
    [[self view] addSubview:underBarGradient];
    [[self view] bringSubviewToFront:underBarGradient];

    menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu.png"] landscapeImagePhone:[UIImage imageNamed:@"menu.png"] style:UIBarButtonItemStyleDone target:self action:@selector(presentMenu)];

    self.navigationItem.leftBarButtonItem = menuButton;

    searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search-icon.png"] landscapeImagePhone:[UIImage imageNamed:@"search-icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentSearch)];

    navigationItemMain.rightBarButtonItem = searchButton;
    self.navigationItemMain.title = @"Welcome to DuoMatch";

    NSLog(@"UI created!");
}

- (IBAction) presentMenu {
    menuButton.tag = 1;
    switch (menuButton.tag) {
        case 0: {
            [_delegate movePanelToOriginalPosition];
            NSLog(@"Case 1!");
            break;
        }

        case 1: {
            [_delegate movePanelRight];
            NSLog(@"Case 2!");
            break;
        }

        default:
            break;
        }
        NSLog(@"Menu presented!");
        NSLog([NSString stringWithFormat:@"Button tag is %ld", (long)menuButton.tag]);
}

- (void) presentSearch {
    NSLog(@"Search presented!");

    [self performSelector:@selector(pushBasicSearchController) withObject:self afterDelay:0.0];
}

ViewController.m - 调用委托方法的类:

[_delegate movePanelRight];

MainViewController.m是我要调用的代码行。方法movePanelRight位于NSLog中。 [self setupView]和应用程序本身显示代码未运行,但正在运行movePanelRight(在MainViewController.m的viewDidLoad中),因此委托似乎已有效。

MainViewController.m - 包含#import "MainViewController.h" #import "ViewController.h" #import "SlideMenuController.h" #define CENTER_TAG 1 #define LEFT_PANEL_TAG 2 #define CORNER_RADIUS 4 #define SLIDE_TIMING .25 #define PANEL_WIDTH 60 @interface MainViewController () <ViewControllerDelegate> @property (nonatomic, strong) ViewController *viewController; @property (nonatomic, strong) SlideMenuController *slideMenuController; @property (nonatomic, assign) BOOL showingMenuPanel; @end @implementation MainViewController #pragma mark - #pragma mark View Did Load/Unload - (void)viewDidLoad { [super viewDidLoad]; [self setupView]; } - (void)setupView { // setup center view UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; self.viewController = [sb instantiateViewControllerWithIdentifier:@"navControllerSB"]; self.viewController.view.tag = CENTER_TAG; self.viewController.delegate = self; [self.view addSubview:self.viewController.view]; [self addChildViewController:_viewController]; NSLog(@"View is set up!"); [_viewController didMoveToParentViewController:self]; } - (UIView *)getLeftView { NSLog(@"I got left view!"); // init view if it doesn't already exist if (_slideMenuController == nil) { // this is where you define the view for the left panel UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; self.slideMenuController = [sb instantiateViewControllerWithIdentifier:@"SlideMenuControllerSB"]; self.slideMenuController.view.tag = LEFT_PANEL_TAG; self.slideMenuController.delegate = _viewController; [self.view addSubview:self.slideMenuController.view]; [self addChildViewController:_slideMenuController]; [_slideMenuController didMoveToParentViewController:self]; _slideMenuController.view.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20); } self.showingMenuPanel = YES; // set up view shadows [self showCenterViewWithShadow:YES withOffset:-2]; UIView *view = self.slideMenuController.view; return view; } - (void)movePanelRight // to show left panel { NSLog(@"XXXX Panel moved!"); UIView *childView = [self getLeftView]; [self.view sendSubviewToBack:childView]; [UIView animateWithDuration:SLIDE_TIMING delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ _viewController.view.frame = CGRectMake(self.view.frame.size.width - PANEL_WIDTH, 0, self.view.frame.size.width, self.view.frame.size.height); } completion:^(BOOL finished) { if (finished) { _viewController.menuButton.tag = 0; } }]; NSLog(@"Panel moved!"); } 方法的类:

movePanelRight

再次总结:为什么委托方法ViewController.mdf = pd.DataFrame({'Number': [1, 2, 3, 4, 5], 'X' : ['X1', 'X2', 'X3', 'X3', 'X3'], 'Y' : ['Y2','Y1','Y1','Y1', 'Y2'], 'Z' : ['Z3','Z1','Z1','Z2','Z1']}) Number X Y Z 0 1 X1 Y2 Z3 1 2 X2 Y1 Z1 2 3 X3 Y1 Z1 3 4 X3 Y1 Z2 4 5 X3 Y2 Z1 中运行时未被调用?所有帮助表示赞赏。

0 个答案:

没有答案