-1

I am creating a drawing application where the user can draw using their finger strokes. I am trying to make a button that asks the user if they would like to clear the canvas. This alert has two buttons "Yes" and "No". I have the alert view appearing correctly but I have spent all day trying to figure out how to hook the buttons up to actions. I have so far had no success even after reading and watching from many instructional sources. From everything that I have read I can't understand why it would not be working. I have included UIAlertViewDelegate in my .h file also.

Here is my alert view:

    - (IBAction)clearButton:(id)sender {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Clear Canvas" 
            message:@"Are you sure?" 
            delegate:nil 
            cancelButtonTitle:@"No" 
            otherButtonTitles:@"Yes", nil];

        [alert show];
    }

Here is my clear canvas method:

- (void)clearCanvas:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1)
        drawImage.image = nil;
}

Any help or pointers would be greatly appreciated! I'm self taught and still very much a beginner! Thanks!

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
user2817587
  • 25
  • 2
  • 2
  • 7
  • "From everything that I have read I can't understand why it would not be working" i.e: I have done what I read I am supposed to do. This is what I have come up with after doing many hours of research – user2817587 Jan 04 '14 at 01:22
  • So you know about UIAlertViewDelegate. and if you see that you are passing in nil for the delegate, does it look ok? – vikingosegundo Jan 04 '14 at 01:23
  • I wouldn't know, that what I am hoping someone can clear up for me. I have tried using both self and nil for the delegate. Some readings told me to use self and some said use nil. Should it be self? – user2817587 Jan 04 '14 at 01:25
  • 1
    it should be `self`. And you should implement `-alertView:clickedButtonAtIndex:` – nielsbot Jan 04 '14 at 01:27
  • how do you expect the alert view to know what to do if you aren't providing a object to hook in? – vikingosegundo Jan 04 '14 at 01:27
  • 1
    http://stackoverflow.com/questions/5763581/uialertview-button-action – vikingosegundo Jan 04 '14 at 01:28
  • I'm sorry, I don't know what that means, I am only 13 and am trying to learn this completely of my own back. Could you dumb it down for me? So it should be self? And how do I implement the '-alertView:clickedButtonAtIndex:'? – user2817587 Jan 04 '14 at 01:35
  • it doesn't matter if you are 13 or 63. you must understand it on your own. it don't has to be self. it must be an object conforming to the UIAlertViewDelegate protocol. – vikingosegundo Jan 04 '14 at 01:37
  • Why must I understand it on my own? Am I being tested? I have spent hours researching this, I fail to see what is so wrong with asking for help? If I am learning, does it matter where I get the info from? If someone understands it (i.e. users of this site) surely it makes sense to go to such people for help? – user2817587 Jan 04 '14 at 01:42
  • what does make you believe that if we repeat the things you have read before on and on it will help you anyhow? – vikingosegundo Jan 04 '14 at 01:46
  • Well my application is not working as intended so I am clearly doing something wrong, does that make sense to you? I attempted my own research but still cannot figure it. Surely the logical thing to do would be to go to someone who can point out where I am going wrong so I can learn from my mistakes? I am having a hard time understanding why you are so reluctant to give advice? I welcome any response justifying your reluctance to help someone who wants to learn? – user2817587 Jan 04 '14 at 01:54
  • if you want to play with the big boys you have to play by their rules. I find 4100+ answers for your problem only on stack overflow. how can't you find any? – vikingosegundo Jan 04 '14 at 02:14
  • Look I don't know what you like to do but I don't want to play with boys. I'm just a kid trying to learn. I learned more from the person who told me what i'm doing wrong than from all the previous hours of research I did. 2 minutes of input from a helpful user vs many hours of personal research that didn't help. How can you fail to see the clear winner here? Stop being so uptight, so I asked for help, big deal, I learned what I wanted didn't I? Why are you crying because a 13 year old kid asked for help? Do you consider it "cheating" or something? – user2817587 Jan 04 '14 at 02:43
  • 1
    On SO we expect you to do a fairly big amount of research before you ask a question. you must demonstrate a minimal understanding of the problem you want to solve. you did not do so. You seem to have no idea of the delegate pattern. That is a very basic pattern in iOS development. and further-more: where is jnawaz answer better than the answer I linked before? Your question and any answer to it are unnecessary, as it is answered thousands of time. If you are not mature enough for this community you should look for another that fits better to your needs. SO won't change just to fit to you. – vikingosegundo Jan 04 '14 at 03:04

1 Answers1

1
- (IBAction)clearButton:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Clear Canvas" 
        message:@"Are you sure?" 
        delegate:self 
        cancelButtonTitle:@"No" 
        otherButtonTitles:@"Yes", nil];

    [alert show];
}

Notice the difference in the delegate parameter. You must conform to the delegate you have declared in the .h

Secondly, use the delegate method -alertView:clickedButtonAtIndex

jnawaz
  • 134
  • 3
  • Thank you so much, this is exactly what I needed. You really cleared it up for me. Your assistance is hugely appreciated as I can now move forward! – user2817587 Jan 04 '14 at 01:56