Set actions for buttons in a UIAlertView?

Discussion in 'iOS Development' started by bamhm182, Jun 9, 2009.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I know that you can do this:

    Code:
    	{
    		UIActionSheet *actionSheet = [[UIActionSheet alloc]
    					 initWithTitle:@"Title"
    					 delegate:self
    				 	 cancelButtonTitle:nil
    					 destructiveButtonTitle:nil
    					 otherButtonTitles:@"Button 1", @"Button 2", nil];
    		[actionSheet showInView:self.view];
    		[actionSheet release];
    	}
    }
    
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    	if (buttonIndex == 0)
    	{
            	//actions
    	}
    	
    	if (buttonIndex == 1)
    	{
    		//actions
    	}
    }
    But I'd rather have it in a UIAlertView rather than a UIActionSheet. How can I do that?

    Also, I've got 2 IBActions referring to the same NSArray that I have set up. How can I put in the Array so that it only needs to be set once. I've tried multiple things, but I can't figure it out. Right now I have it set up so that the NSArray is defined in both the Actions, but I'm assuming it would be better if it was just defined once.

    I've got 1 more question, I want to at least attempt to slow the piracy of my app, not that I'd really sell that many anyways, but it never hurts to try. Is there any way to get the link that my app will have before sending it through the app approval process? I want to have a button that will take the pirates to my app in iTunes.

    EDIT: Just thought of 1 more thing, how can I set a border for my UIScrollView? For example, I've got boxes all the way down to the bottom of my UIScrollView, and when I click on the bottom text box, the keyboard covers the last few text boxes. Is there a way to set it so the user can scroll down 1700 pixels, but that the keyboard can tell it to move to about 1800 pixels?
  2. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    In UIAlert.h, there are two classes, UIActionSheet and UIAlertView, UIAlertView.

    Declare the array in the @interface of your class.

    I guess that you could make it that when any of the text boxes becomes first responder, the UIScrollView frame "shrinks".
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I'm not sure what you meant by the first part. I already tried putting in this:

    Code:
        {
            UIAlertView *alert = [[UIAlertView alloc]
                         initWithTitle:@"Title"
                         delegate:self
                          cancelButtonTitle:nil
                         destructiveButtonTitle:nil
                         otherButtonTitles:@"Button 1", @"Button 2", nil];
            [alert show];
            [alert release];
        }
    }
    
    - (void)alert:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0)
        {
                //actions
        }
        
        if (buttonIndex == 1)
        {
            //actions
        }
    }
    Nothing happens, is that what you meant?

    As for the 2nd part, thanks! In case any else who needs help with this comes across this thread, you need to have it set up like this:

    Code:
    @interface ViewController : UIViewController <UIActionSheetDelegate>
    {
    NSArray *textFields = [NSArray arrayWithObjects:...];
    }
    
    @property (nonatomic, retain) NSArray *textFields;
    Than have this in the .m:

    Code:
    @synthesize textFields;
    
    -(IBAction)ActionOne:(id)sender{
    
    	for(int i=0; i<45; i++)
    	{
    //This is how you reference the array.
    		NSArray *networkTextFields = networkTextFields;
    //Than make whatever references to textFields you want.
    //For example:
            [[networkTextFields objectAtIndex:i] resignFirstResponder];
        }
    }
    For the third part, how could I do that?

    I'm sorry for all the questions, I'm completely new to coding.

    EDIT: I thought I knew what I was doing on the second part. Would you mind looking over my code and see what I'm doing wrong? I get 2 errors. "/ViewController.h:68: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token" twice.
  4. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    What is "NSArray *textFields = [NSArray arrayWithObjects:...];" is doing in the header file?

    It should be NSArray *textFields; in the header file. maybe put textFields = [NSArray arrayWithObjects:...]; inside of the viewDidLoad in .m file.
  5. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    That didn't work either Noles.

    EDIT: I got the first problem sorted.

    The - (void) needs to be named alertView, not alert. This code works:

    Code:
        {
            UIAlertView *alertView = [[UIAlertView alloc]
                         initWithTitle:@"Title"
                         delegate:self
                          cancelButtonTitle:nil
                         destructiveButtonTitle:nil
                         otherButtonTitles:@"Button 1", @"Button 2", nil];
            [alert show];
            [alert release];
        }
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0)
        {
                //actions
        }
        
        if (buttonIndex == 1)
        {
            //actions
        }
    }
  6. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Usually you can do it with the UIActionSheet snippet, it applies to both, what it does is read the button index -- Top button = 0, Bottom Button (if there are only two) = 1 -- You could probably make three buttons, leaving the top button blank to close it and apply actions to the other two.

    Code:
    - (IBAction)HERE_IS_YOUR_IB_ACTION_TO_SHOW_ALERT:(id)sender {
    	// open an alert with two custom buttons
    	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@""
    												   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Function 1", @"Function 2", nil];
    	[alert show];
    	[alert release];
    }
    
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    	// the user clicked one of the OK/Cancel buttons
    	if (buttonIndex == 0)
    	{
    		//here is where we can close it
    	}
    	if (buttonIndex == 1)
    	{
    		//add button 1's function here...
    	}
    	if (buttonIndex == 2)
    	{
    		//annnnnnnnnnnnnnnnnnnnd button 2 here...
    	}
    }
    
    Hope it helps

    Please Register or Log in to view images



    CT

    EDIT: It looks like (because you are creating a UITextField Array) that you want to add Text Field's to the UIAlertView? It's simple enough, just programatically initialize it, and add it [alert addSubview:textField1]; (You will need to make it via CGRect and no longer than 10px off the side of the alert) and then on the destruction of the alert, Release it.
  7. Datsafail

    Datsafail Member

    Joined:
    May 30, 2010
    Messages:
    33
    Likes Received:
    0
    Device:
    iPhone 4S (White)
    How would I do the actions without using the - (void) thingy?
  8. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    You can't. The void statement handles the action for all the buttons on the UIAlertView class. Unless you forcibly add a UIButton to the AlertView (impractical, inefficient and very dirty) and add an @selector, you're stuck with the -(void) method.

    Please Register or Log in to view images

    Hope this helped.
  9. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    Apple could add block functionality in iOS 5.1, maybe.

    </wishful thinking

    Please Register or Log in to view images

    >

Share This Page