无法获得滑动菜单按钮才能正常工作? (IOS)

时间:2014-05-17 18:45:45

标签: ios objective-c unrecognized-selector

我正在尝试使操作栏左上角的按钮工作,我可以让幻灯片工作但单击按钮调用相同的方法,但它继续只调用方法的else部分。 / p>

这是滑出抽屉的方法:

-(void)drawerAnimation {
    NSLog(@"drawer animation called");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:-5];

    CGFloat new_x = 0;
    if (menuDrawer.frame.origin.x < self.view.frame.origin.x ) {
        NSLog(@"drawer animation called menu origin if");
        new_x = menuDrawer.frame.origin.x + menuDrawerWidth;
    } else {
        NSLog(@"drawer animation called menu origin else");
        new_x = menuDrawer.frame.origin.x - menuDrawerWidth;
    }
    menuDrawer.frame = CGRectMake(new_x, menuDrawer.frame.origin.y, menuDrawer.frame.size.width, menuDrawer.frame.size.height);

    [UIView commitAnimations];

}

这是我添加按钮的方式,但我确实有一个三角形黄色警告标志

incompatible pointer to integer conversion UIBarButtonItemStyle

这是另一个.m文件中的按钮,其中抽屉动画位于父导航控制器中:

//add navigation top left bar items
    UIBarButtonItem *menuBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:self target:self action:@selector(drawerAnimation)];

    //add items to array
    NSArray *actionButtonItems = @[shareItem, cameraItem];
    NSArray *leftActionButtonItems = @[menuBtn];

    //add array to navigation bar
    self.navigationItem.rightBarButtonItems = actionButtonItems;
    self.navigationItem.leftBarButtonItems = leftActionButtonItems;

滑动菜单控制器代码的其余部分:

@interface SlidingMenuViewController ()

@end


@implementation SlidingMenuViewController
@synthesize menuDrawerWidth, menuDrawerX, recognizer_open, recognizer_close, mainTitle, menuItems;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    int statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    menuDrawerWidth = self.view.frame.size.width * 0.75;
    menuDrawerX = self.view.frame.origin.x - menuDrawerWidth;
    menuDrawer = [[UIView alloc]initWithFrame:CGRectMake(menuDrawerX, self.view.frame.origin.y+statusBarHeight, menuDrawerWidth, self.view.frame.size.height-statusBarHeight)];
    menuDrawer.backgroundColor = [UIColor redColor];


    //initialize open and close
    recognizer_close = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

    recognizer_open = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

    //for opening and closing drawer
    recognizer_close.direction = UISwipeGestureRecognizerDirectionLeft;
    recognizer_open.direction = UISwipeGestureRecognizerDirectionRight;


    //add to view
    [self.view addGestureRecognizer:recognizer_open];
    [self.view addGestureRecognizer:recognizer_close];

    //name title for menu and add to the drawer view
    UILabel *menuTitle = [[UILabel alloc] initWithFrame:CGRectMake(60.0f, 5.0f, 200.0f, 50.0f)];
    menuTitle.text = @"Menu Options";
    [menuDrawer addSubview:menuTitle];

    //scrollview to contain the buttons of the actions if more drawer items then what is on screen
    UIScrollView *m_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(3.0f, 65.0f, menuDrawerWidth, menuDrawer.frame.size.height)];

    //set options for scrollview design
    [m_scrollView setScrollEnabled:YES];
    [m_scrollView setShowsHorizontalScrollIndicator:NO];
    [m_scrollView setShowsVerticalScrollIndicator:YES];
    [m_scrollView setBackgroundColor:[UIColor clearColor]];
    [m_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault];
    [m_scrollView setCanCancelContentTouches:NO];
    [m_scrollView setClipsToBounds:YES];


    //buttons measurement for each menu item
    float originOfButtons = 10.0f;
    float buttonWidth = 227.0f;
    float buttonHeight = 50.0f;
    int buttonSeparator = 10;

    menuItems = [[NSArray alloc]initWithObjects:@"Home", @"Explore", @"Share", @"Profile", @"Settings", nil];
    for(int b=0; b <[menuItems count];b++) {
        UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake(3.0f, originOfButtons, buttonWidth, buttonHeight)];
        mybutton.backgroundColor = [UIColor blueColor];
        [mybutton setTag:b];
        [mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
        [mybutton setSelected:false];
        [mybutton addTarget:self action:@selector(menuSelect:) forControlEvents:UIControlEventTouchUpInside];


        //add button to scrollview
        [m_scrollView addSubview:mybutton];


        //add extra button height and separator for each one
        originOfButtons +=(buttonHeight + buttonSeparator);


    }


    //bounds of scrollview and add it to content size
    [m_scrollView setContentSize:CGSizeMake([m_scrollView bounds].size.width, originOfButtons + 85)];

    [menuDrawer addSubview:m_scrollView];
    [self.view addSubview:menuDrawer];




}

-(void)viewDidAppear:(BOOL)animated {

    // Get the stored data before the view loads
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];


    //if values exist then already logged in else take to login screen
    if([[[defaults dictionaryRepresentation] allKeys] containsObject:@"userId"]){

        NSLog(@"userId found");
    } else {
        NSLog(@"userId NOT found");
        StarterViewController *hc=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Starter"];
        [self presentViewController:hc animated:YES completion:nil];

    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)createMenuButton {
    [self drawerAnimation];


}


- (void)menuSelect:(id)sender {
    NSString *selectedTitle = [menuItems objectAtIndex:[sender tag]];

    UIButton *button = (UIButton*) sender;
    mainTitle.text = selectedTitle;
    switch (button.tag) {

        case 0: {
            NSLog(@"settings clicked");
            if(![self.visibleViewController isKindOfClass:[SettingsViewController class]]) {
                SlidingMenuViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MenuSettings"];
                [self presentViewController:viewController animated:YES completion:nil];
            }
            break;
        }


        default:
            break;
    }

    [self drawerAnimation];
}


-(void)handleSwipes:(UISwipeGestureRecognizer *)sender {
    //calls the animation
    [self drawerAnimation];

}


-(void)drawerAnimation {
    NSLog(@"drawer animation called");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:-5];

    CGFloat new_x = 0;
    if (menuDrawer.frame.origin.x < self.view.frame.origin.x ) {
        NSLog(@"drawer animation called menu origin if");
        new_x = menuDrawer.frame.origin.x + menuDrawerWidth;
    } else {
        NSLog(@"drawer animation called menu origin else");
        new_x = menuDrawer.frame.origin.x - menuDrawerWidth;
    }
    menuDrawer.frame = CGRectMake(new_x, menuDrawer.frame.origin.y, menuDrawer.frame.size.width, menuDrawer.frame.size.height);

    [UIView commitAnimations];

}

@end

错误更新

新代码:( drawerSlide是在.h之上添加的BOOL,并在滑动菜单控制器的.m文件中合成)

if(!self.drawerSlide){     NSLog(@&#34;抽屉动画,称为菜单原点,如果&#34;);     new_x = menuDrawer.frame.origin.x + menuDrawerWidth;     self.drawerSlide = YES; } else {     NSLog(@&#34;抽屉动画,称为菜单原点&#34;);     new_x = menuDrawer.frame.origin.x - menuDrawerWidth;     self.drawerSlide = NO; }

2014-05-17 22:31:25.882 Stand[8099:60b] -[ExploreTableViewController drawerAnimation]: unrecognized selector sent to instance 0xa47e4e0
2014-05-17 22:31:25.886 Stand[8099:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ExploreTableViewController drawerAnimation]: unrecognized selector sent to instance 0xa47e4e0'
*** First throw call stack:
(
    0   CoreFoundation                      0x01e311e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01bb08e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01ece243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x01e2150b ___forwarding___ + 1019
    4   CoreFoundation                      0x01e210ee _CF_forwarding_prep_0 + 14
    5   libobjc.A.dylib                     0x01bc2880 -[NSObject performSelector:withObject:withObject:] + 77
    6   UIKit                               0x008723b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    7   UIKit                               0x00b5f8df -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
    8   libobjc.A.dylib                     0x01bc2880 -[NSObject performSelector:withObject:withObject:] + 77
    9   UIKit                               0x008723b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    10  UIKit                               0x00872345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    11  UIKit                               0x00973bd1 -[UIControl sendAction:to:forEvent:] + 66
    12  UIKit                               0x00973fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    13  UIKit                               0x00973243 -[UIControl touchesEnded:withEvent:] + 641
    14  UIKit                               0x008b1ddd -[UIWindow _sendTouchesForEvent:] + 852
    15  UIKit                               0x008b29d1 -[UIWindow sendEvent:] + 1117
    16  UIKit                               0x008845f2 -[UIApplication sendEvent:] + 242
    17  UIKit                               0x0086e353 _UIApplicationHandleEventQueue + 11455
    18  CoreFoundation                      0x01dba77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    19  CoreFoundation                      0x01dba10b __CFRunLoopDoSources0 + 235
    20  CoreFoundation                      0x01dd71ae __CFRunLoopRun + 910
    21  CoreFoundation                      0x01dd69d3 CFRunLoopRunSpecific + 467
    22  CoreFoundation                      0x01dd67eb CFRunLoopRunInMode + 123
    23  GraphicsServices                    0x03bf05ee GSEventRunModal + 192
    24  GraphicsServices                    0x03bf042b GSEventRun + 104
    25  UIKit                               0x00870f9b UIApplicationMain + 1225
    26  Stand                               0x00017d0d main + 141
    27  libdyld.dylib                       0x0257d701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:0)

所以,问题(本来可以更简洁地说明)是else分支永远不会在这里选择:

if (menuDrawer.frame.origin.x < self.view.frame.origin.x ) {
    NSLog(@"drawer animation called menu origin if");
    new_x = menuDrawer.frame.origin.x + menuDrawerWidth;
} else {
    NSLog(@"drawer animation called menu origin else");
    new_x = menuDrawer.frame.origin.x - menuDrawerWidth;
}

因此,必须是menuDrawer.frame.origin.x < self.view.frame.origin.x始终为真的情况。因此,如果那不是您所期望的,那么您正在测试错误的东西。

您需要使用更多日志记录(或断点并检查调试器中的变量)来查看这些值实际上是什么。然后你需要考虑为什么它们不是你所期望的。

一种可能性是您可能不应该将menuDrawer.frameself.view.frame进行比较。如果(我假设)menuDrawerself.view的子视图,那么比较应该是self.view.bounds,而不是self.view.frame。但是,如果没有进一步的信息,就无法确定。

另一个问题(这可能是实际原因)是您要比较menuDrawerself.view的左边缘。您的代码中没有任何内容可以保证当幻灯片动画发生时,menuDrawer的左边缘将self.view的左边缘更靠右边。如果即使只有一小部分menuDrawer仍在屏幕左侧,else将永远不会成立。如果是这样,你需要考虑更好的比较测试!你可能做的是比较,而不是self.view,而只是比较一些已知的整数值(可能是一个小的负数),其中menuDrawer.frame.origin.x 的值会改变哪个这个价值的一面是它的。然而,会做什么只是在我们每次滑入或滑出时抛出BOOL实例变量,以便BOOL跟踪我们是否进入,然后我们可以进来如果我们在这里,那就出去了:

if (!self.in) {
    NSLog(@"drawer animation called menu origin if");
    new_x = menuDrawer.frame.origin.x + menuDrawerWidth;
    self.in = YES;
} else {
    NSLog(@"drawer animation called menu origin else");
    new_x = menuDrawer.frame.origin.x - menuDrawerWidth;
    self.in = NO;
}