UIActionSheet Cancel Button not working

Discussion in 'iOS Programming' started by newtoiphonesdk, Sep 2, 2010.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I have a UIActionSheet implemented into my code. It's setup with Email and Facebook as the two other buttons, and they both work fine. However, with cancel, sometimes it will work right away, sometimes it won't work at all, and sometimes it takes it a minute before it will work. Here is the code I am using for the ActionSheet:
    Code:
    -(IBAction)action:(id)sender {
    	
    	UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Share article with a friend..." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Email", @"Facebook", nil];
    	[popup showInView:[self view]];
    	[popup release];
    	
    }
    - (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {
    	
    	switch (buttonIndex) {
    		case 0:
    		{self.currentURL = links.request.URL.absoluteString;
    			self.title = [links stringByEvaluatingJavaScriptFromString:@"document.title"];
    			// This sample can run on devices running iPhone OS 2.0 or later  
    			// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 
    			// So, we must verify the existence of the above class and provide a workaround for devices running 
    			// earlier versions of the iPhone OS. 
    			// We display an email composition interface if MFMailComposeViewController exists and the device can send emails.
    			// We launch the Mail application on the device, otherwise.
    			
    			Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    			if (mailClass != nil)
    			{
    				// We must always check whether the current device is configured for sending emails
    				if ([mailClass canSendMail])
    				{
    					[self displayComposerSheet];
    				}
    				else
    				{
    					[self launchMailAppOnDevice];
    				}
    			}
    			else
    			{
    				[self launchMailAppOnDevice];
    			}
    		}
    			
    			break;
    		case 1:
    		{self.currentURL = links.request.URL.absoluteString;
    			self.title = [links stringByEvaluatingJavaScriptFromString:@"document.title"];
    			_posting = YES;
    			// If we're not logged in, log in first...
    			if (![_session isConnected]) {
    				self.loginDialog = nil;
    				_loginDialog = [[FBLoginDialog alloc] init];	
    				[_loginDialog show];	
    			}
    			// If we have a session and a name, post to the wall!
    			else if (_facebookName != nil) {
    				[self postToWall];
    			}
    			// Otherwise, we don't have a name yet, just wait for that to come through.
    		}
    			
    			break;
    			
    			
    	}	
    	
    	
    }
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Button indices start at 0.

    What is the index of your Cancel button? Your other buttons? How would you confirm this?

    Add code that shows the value of the button index, to confirm that what you expect to happen is really what is happening.

    Add code that shows the correlation between button index and button title. Use the buttonTitleAtIndex: method of UIActionSheet.

    You should get in the habit of writing code that confirms your expectation of what is happening. Then when something unexpected happens, that will be your first response: confirm the expectation.

    You can also use the debugger to confirm expectations, by inspecting variable values at appropriate points. If something isn't what you expect, you at least know what is really happening. The next step is usually to check the reference docs, to make sure that your expectation is valid.
     
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    I did put in my code what I expect to happen. It's written in case statements. My UIActionSheet code shows the cancel button, and then the email and facebook buttons. It's all there right in the original code. I was under the impression that when you implement a UIActionSheet with a cancel button, you don't have to program the Cancel button to anything. The other two buttons are working just fine. I know they work fine because when I click them, they do what they're supposed to.

    I ask a question describing the problem, and I'm told I need to include code in my question for people to see what the problem is. I include the code in my question and I'm told I should have included logs so that I could figure out the question on my own. If I could figure this out on my own, I wouldn't be asking questions here.
     

Share This Page