Displaying a UIAlertView in a Tab-Based Application?

Discussion in 'iOS Development' started by Noise..., Oct 5, 2009.

  1. Noise...

    Noise... Banned

    Joined:
    Dec 21, 2008
    Messages:
    421
    Likes Received:
    1
    Device:
    3G iPod touch
    I'm working on a Tab-Based app that uses multiple UIWebViews (one on each tab).

    It works fine - aside from one thing; the AlertView for lack of internet connection.

    I'm using the same code that I used in a View-Based app (which it worked perfectly in) for each ViewController.

    Is there something special that needs to be done with a tab-based app to get an alertView working when there's no internet connection?

    I'd appreciate any help!
  2. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Why would there be?
  3. Noise...

    Noise... Banned

    Joined:
    Dec 21, 2008
    Messages:
    421
    Likes Received:
    1
    Device:
    3G iPod touch
    Because it's a different form of application?

    Obviously something isn't connecting, or it would be working in the Tab-Based app like it is in the View-Based app. It's the same code applied to each.
  4. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    There should be no problem. Objective-C is Objective-C.
  5. Noise...

    Noise... Banned

    Joined:
    Dec 21, 2008
    Messages:
    421
    Likes Received:
    1
    Device:
    3G iPod touch
    Is there a certain way I should be connecting things in Interface Builder? I'm just perplexed at why it's not working in the Tab-Based app, but is working flawlessly in the View-Based app.
  6. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    You mean the one that the iPhone throws automatically, or you are checking it and throwing it?

    I don't see why it wouldn't show correctly.
  7. Noise...

    Noise... Banned

    Joined:
    Dec 21, 2008
    Messages:
    421
    Likes Received:
    1
    Device:
    3G iPod touch
    Here's the code I'm using.

    ViewController.h

    Code:
    #import <UIKit/UIKit.h>
    
    
    @interface SecondViewController : UIViewController <UIWebViewDelegate, UIAlertViewDelegate>
    {
    	UIWebView *google;
    	
    	NSURL *url;
    	
    	UIBarButtonItem *backButton;
    	UIBarButtonItem *forwardButton;
    }
    
    @property (nonatomic, retain) IBOutlet UIWebView *google;
    @property (nonatomic, retain) NSURL	*url;
    
    @property (nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
    @property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
    
    
    
    @end
    ViewController.m

    Code:
    #import "SecondViewController.h"
    #import <CFNetwork/CFNetwork.h>
    
    #pragma mark Private Methods
    @interface SecondViewController ()
    
    - (void)goBack;
    - (void)goForward;
    
    @end
    
    #pragma mark -
    @implementation SecondViewController
    
    @synthesize google;
    @synthesize url;
    @synthesize backButton;
    @synthesize forwardButton;
    
    #pragma mark -
    #pragma mark Dealloc and Memory Methods
    
    - (void)dealloc 
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[google release], google = nil;
    	[url release], url = nil;
    	[backButton release], backButton = nil;
    	[forwardButton release], forwardButton = nil;
    	
        [super dealloc];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    - (void)viewDidUnload
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    - (void)didReceiveMemoryWarning 
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
        [super didReceiveMemoryWarning];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    - (void)setView:(UIView *)aView
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
        if (!aView) { // view is being set to nil
            // set outlets to nil, e.g.
            //self.anOutlet = nil;
        }
        // Invoke super's implementation last
        [super setView:aView];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    #pragma mark -
    #pragma mark Initialization and UI Creation Methods
    
    - (id)init
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	if (self = [super initWithNibName:@"SecondViewController" bundle:nil])
    	{
    		[self setHidesBottomBarWhenPushed: YES];
    	}
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    	return self;
    }
    
    - (void)viewDidLoad 
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
    																		  target:self
    																		  action:@selector(goBack)];
    	
    	UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward
    																		  target:self
    																		  action:@selector(goForward)];
    	
    	UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
    																			 target:nil
    																			 action:nil];
    	
    	[self setBackButton:back];
    	[self setForwardButton:forward];
    	
    	[self setToolbarItems:[NSArray arrayWithObjects:[self backButton], space, [self forwardButton], nil]];
    	
    	[back release], back = nil;
    	[forward release], forward = nil;
    	[space release], space = nil;
    	
        [super viewDidLoad];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    - (void)viewWillAppear:(BOOL)animated 
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[[self navigationController] setToolbarHidden:NO animated:YES];
    	
    	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    	[[self google] loadRequest:request];
    	[request release], request = nil;
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[[self google] stopLoading];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    	
    //- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    //{
    //	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    //	
    //	
    //	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    //}
    //
    //- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    //{
    //	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    //	
    //	
    //	
    //	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    //}
    
    #pragma mark -
    #pragma mark UI Response Methods
    
    - (void)goBack
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[[self google] goBack];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    - (void)goForward
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[[self google] goForward];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    #pragma mark -
    #pragma mark UIWebView Delegate Methods
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    	[[self backButton] setEnabled:[webView canGoBack]];
    	[[self forwardButton] setEnabled:[webView canGoForward]];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    
    - (void)webViewDidStartLoad:(UIWebView *)webView
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    	[[self backButton] setEnabled:[webView canGoBack]];
    	[[self forwardButton] setEnabled:[webView canGoForward]];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    - (void)webView:(UIWebView *)webView 
    didFailLoadWithError:(NSError *)error
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	NSLog(@"The error is: %@", error);
    	
    	[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    	
    	if ([error code] != kCFURLErrorCancelled)
    	{
    		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data not available"
    														message:@"Unable to communicate with web site\nVerify your internet connection"
    													   delegate:self
    											  cancelButtonTitle:@"OK"
    											  otherButtonTitles:nil];
    		[alert show];
    		[alert release], alert = nil;
    	}
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    
    #pragma mark -
    #pragma mark UIAlertView Delegate Methods
    
    - (void)alertView:(UIAlertView *)alertView 
    clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    	NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    	
    	[[self navigationController] popViewControllerAnimated:YES];
    	
    	NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
    }
    	
    	
    	
    @end

    Is there anything in there that would be preventing it from working?
  8. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    UIAlertView is just a modal view--It won't affect the other views in your app, save for disabling interaction with them whilst it is being shown.
  9. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    No it's not. It's just a different template. It'll work just fine, you're doing something wrong.
  10. Noise...

    Noise... Banned

    Joined:
    Dec 21, 2008
    Messages:
    421
    Likes Received:
    1
    Device:
    3G iPod touch
    What could be wrong that would make the alert not pop up?

Share This Page