What's wrong with this code?

Discussion in 'iOS Programming' started by nashyo, Feb 1, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    Code:
    - (IBAction)cardiac:(id)sender 
    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:nil];
        
        [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
        
        CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
        
        UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
        pickerView.showsSelectionIndicator = YES;
        pickerView.dataSource = self;
        pickerView.delegate = self;
        
        [actionSheet addSubview:pickerView];
        
        UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
        closeButton.momentary = YES; 
        closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
        closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
        closeButton.tintColor = [UIColor blackColor];
        [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
        [actionSheet addSubview:closeButton];
        
        [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
        
        [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
    }
    The app crashes with [ContentViewController dismissActionSheet:]: unrecognized selector sent to instance 0x6a57bf0, when I select closeButton
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Because ContentViewController doesn't respond to dismissActionSheet:.

    So write a method called that.
     
  3. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #3
    Does self have a dismissActionSheet: method ?

    Error message is quite explicit. You have not given us enough information to tell you what's wrong.
     
  4. chown33, Feb 1, 2012
    Last edited: Feb 1, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    What's wrong with the code? Let's make a short list.

    1. You didn't tell us what class the posted method is in. Is it the same as the class that was named in the crash? Something else?

    2. You didn't say whether it's running in the simulator or the device.

    3. You didn't say whether it's running with or without ARC.

    4. You didn't say whether it compiles without warnings. If you're ignoring a warning, the answer could easily be right there.

    5. You didn't show a stack dump showing what lead to the crash.


    This is just a short list of missing information, or extremely simple things to try to debug it.
     
  5. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    1.Yes, same as mentioned in crash
    2.simulator
    3.running with ARC
    4.no warnings
    5.I confess I don't know how to debug very well

    but I figured out the problem, by changing one small portion of the code

    Code:
    [closeButton addTarget:actionSheet action:@selector(dismissWithClickedButtonIndex:animated:) forControlEvents:UIControlEventValueChanged];
    the picker view does not dismiss with animation, so it seems. it just disappears. i'll try and figure this out.

    next time i will post the above observations.
     
  6. chown33, Feb 1, 2012
    Last edited: Feb 1, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Honestly, the other replies were far more accurate than mine.


    First, do you realize that in your original code, your class needed a method named dismissActionSheet: ? If not, then you haven't figured out the problem, you just avoided that problem by creating a different one.

    Did your class have that method? KnightWRX asked that question, but you didn't answer it. If your class didn't have that method, then you'd need to write it. If your class did have method, you didn't show any code for it. The failure might have been because the spelling of the methods was slightly different. No one knows unless you tell us exactly what you did.


    Second, the method you're telling closeButton to run for control events needs a specific number and type of parameters.

    You can't substitute a one-parameter method with a two-parameter method and expect it to work. Yet that's exactly what your new code does. The method dismissWithClickedButtonIndex:animated: needs two parameters. Is it getting them? What are the parameter values? If you don't know, then you need to study how action sheets in particular use their delegate methods, and also study how methods are defined for delegates.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Perhaps take this as a cue to learn how to do it better then.
     
  8. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #8
    I have studied the delegate and I don't understand. I didn't just copy and paste them at random.

    Nevermind. I will keep reading.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    If you expect a useful reply, then you have to describe what it is you don't understand.

    Do you understand that the target object needs the method you gave as the selector? In particular, that it needs to actually have the method whose selector you're telling the control to call. Logically, if you tell the control "call this method on that target object", doesn't it make sense that the target object needs to actually have the method you identified?

    Maybe you don't have a clear understanding of what a method is, or what a selector means in regard to a method. Or maybe you don't understand how objects have methods, when the code you write is in a class, so perhaps you don't understand the relationship between objects and classes.

    I'm just guessing, because you haven't given us anything to go on.
     
  10. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #10
    Honestly, don't worry about it. I won't make the mistake again.
     

Share This Page