Switching Views with NIB to add log in screen to APP

Discussion in 'iOS Programming' started by LostinMac, Oct 20, 2012.

  1. LostinMac, Oct 20, 2012
    Last edited: Oct 20, 2012

    LostinMac macrumors newbie

    Joined:
    Oct 20, 2012
    #1
    greetings all,

    My team and I are having a problem adding a login screen to an app that we know works.
    Right now the system will through the error of unidentified selector to instance error.
    If we take out the login view controller and all subsequent information the system will get the information from our JSON server and present it properly but we just can't add the login XIB and associated ViewControllers to the app. We have been banging our heads against this wall for over a week now and anythign we try either will not present the UITABLEView Controller to the screen or gives us this werror.

    We are somewhat new to XCODE and IPHONE programming but not newbies to programming logic, just xcode and iphone logic.
    We have several different versions of this app that are working on and we want get the pieces to work together.

    here is the code

    thanks for the help in advance.

    John

    appdelegate.h
    Code:
    //
    //  AppDelegate.h
    
    #import <UIKit/UIKit.h>
    
    @class ViewController;
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    @property (strong, nonatomic) ViewController *viewController;
    
    @end
    
    
    appdelegate.m
    Code:
    //
    //  AppDelegate.m
    
    
    #import "AppDelegate.h"
    
    #import "LoginViewController.h"
    #import "ViewController.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[ViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application
    {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application
    {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    - (void)applicationDidBecomeActive:(UIApplication *)application
    {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application
    {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    @end
    
    
    LoginViewController.h
    Code:
    //
    //  LoginViewController.h
    
    #import <UIKit/UIKit.h>
    #import "ViewController.h"
    
    @interface LoginViewController : UIViewController
    {
        ViewController *viewcontroller;
        
    }
    
    - (IBAction)continue:(id)sender;
    @end
    
    
    LoginViewController.m
    Code:
    //
    //  LoginViewController.m
    
    
    #import "LoginViewController.h"
    
    @interface LoginViewController ()
    
    @end
    
    @implementation LoginViewController
    - (IBAction)continue:(id)sender
    {
        ViewController * viewcontroller = [[ViewController alloc]init];
        [self.navigationController pushViewController: viewcontroller animated:YES];
        
        [self presentModalViewController:viewcontroller animated:YES];
    
        
    }
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    
    
    ViewController.h
    Code:
    //
    //  ViewController.h
    
    #import <UIKit/UIKit.h>
    
    
    
    @interface ViewController : UIViewController {
        IBOutlet UITableView *mainTableView;
        
        NSArray *news;
        NSMutableData *data;
    }
    @property (retain, nonatomic) NSString *newurl;
    @end
    
    
    ViewController.m
    Code:
    //
    //  ViewController.m
    
    #import "LoginViewController.h"
    #import "ViewController.h"
    #import "DetailViewController.h"
    
    
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize newurl;
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.title = @"papertracking";
        
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        
        NSURL *url = [NSURL URLWithString:@"http://10.4.4.11/lost/a1.php?Fone=5125551212&pincode=12&edit=Register"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
    	// Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        data = [[NSMutableData alloc] init];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
    {
        [data appendData:theData];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
        [mainTableView reloadData];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [errorView show];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }
    
    - (int)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [news count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
        
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
        }
        
        cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"dname"];
        cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"dlloc"];
        
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
        detailViewController.title = [[news objectAtIndex:indexPath.row] objectForKey:@"dname"];
        detailViewController.newsArticle = [news objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:detailViewController animated:YES];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    @end
    
    
    
    DetailViewController.h
    Code:
    //
    //  DetailViewController.h
    
    
    #import <UIKit/UIKit.h>
    
    @interface DetailViewController : UIViewController {
        NSDictionary *newsArticle;
        
        IBOutlet UILabel *titleLabel;
        IBOutlet UILabel *timeLabel;
        
        IBOutlet UITextView *descTextView;
    }
    
    @property (nonatomic, copy) NSDictionary *newsArticle;
    
    @end
    
    
    DetailViewController.m
    Code:
    //
    //  DetailViewController.m
    
    
    #import "DetailViewController.h"
    
    @interface DetailViewController ()
    
    @end
    
    @implementation DetailViewController
    @synthesize newsArticle;
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        
        titleLabel.text = [newsArticle objectForKey:@"title"];
        timeLabel.text = [newsArticle objectForKey:@"date_string"];
        
        descTextView.text = [newsArticle objectForKey:@"article"];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    
    @end
    
    
     
  2. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #2
    You are initialising ViewController with LoginViewController's xib.

    Should be like this:

    Code:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        self.window.rootViewController = navController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    
     
  3. LostinMac thread starter macrumors newbie

    Joined:
    Oct 20, 2012
    #3
    CodeBreaker,

    Thanks for the suggestion

    but we get this error
    Code:
    2012-10-21 11:46:07.730 JSONNews[2039:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <LoginViewController: 0x681ca10>.'
    
    when we push the login button on the front screen. We have also tried to change to a subview to it in order to present the table but it just won't go.

    Thanks

    Got any other suggestions

    Thanks
    John
     
  4. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #4
    You are pushing a view controller in the navigation stack and then presenting it as a modal view controller. You can't do both. Either present it as a modal controller or push it in the navigation stack.

    Code:
    - (IBAction)continue:(id)sender
    {
        ViewController * viewcontroller = [[ViewController alloc]init];
        [self.navigationController pushViewController: viewcontroller animated:YES];
        
        // Do not do this:
        // [self presentModalViewController:viewcontroller animated:YES];
    
        
    }
    
     

Share This Page