PDA

View Full Version : [Resolved] trying to load view.. but am completely lost




webznz
Apr 20, 2011, 09:04 PM
Hi, I am trying to load a new view form an alertView button, I am catching the button click fine as I see my NSLog coming from the method the button instantiates.

However I'm just abit lost as to how to get the new view to load... Mainly because I'm doing a few things I haven't done before like creating my own classes I call from others in an attempt to keep my code clean and transportable...

basically I have one NSObject (pingConnection) that is being called from my appDelegate as soon as the app starts, that sends a request to the server gets a reply and depending on the reply code I get back I do one of several different alertViews I have set in my (alerts) class.
Then when my alert is on screen and in this instance the button titled OK is pressed by the user which will then be used to load a new view, heres the part where I try and load my new view

- (void)pleaseRegisterDevice {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Register Device"
message:@"click OK to register"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[alert autorelease];
[alert show];

}


//Catch pleaseRegisterDevice method
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"OK"]) {

NSLog(@"msg from alertView method");
//open new window
registerDeviceViewController *_regDevice = [[registerDeviceViewController alloc] initWithNibName:@"registerDeviceViewController" bundle:nil];
[_regDevice.navigationController pushViewController:_regDevice animated:YES];
[_regDevice release];
_regDevice = nil;


}
else {
NSLog(@"didnt work");
}

}

I'm not getting any errors but the view is not loading.. I just have no idea why not as I have limited experience with objective C, if someone has any ideas on this I would be greatfull if you could share with me.



Shawnpk
Apr 21, 2011, 01:23 AM
Just so you know, class names should start with a capital letter. Also, using underscores is bad form. Now to your code. Do you have a class called registerDeviceViewController? Do you have a nib named registerDeviceViewController? Did you import the class you are trying to instantiate?

jnoxx
Apr 21, 2011, 08:35 AM
And u are sure u actually have a navigationController to push it on.
And like the guy said, classNames mostly start with an Uppercase letter. (allways actually.)

dejo
Apr 21, 2011, 09:24 AM
Also, using underscores is bad form.
Why do you say that? I use them all the time, for the instance variables that back my properties. See this post (http://forums.macrumors.com/showpost.php?p=11512249&postcount=7) of mine for more details.

As for the OP's coding style, they should probably give this a read: Coding Guidelines for Cocoa (http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html)

Shawnpk
Apr 21, 2011, 09:45 AM
Why do you say that? I use them all the time, for the instance variables that back my properties. See this post (http://forums.macrumors.com/showpost.php?p=11512249&postcount=7) of mine for more details.

Apple has traditionally used a single underscore prefix to denote a private instance variable (a common style in object-oriented languages). This was taken to imply that everyone should prefix their ivars with underscores until Apple pointed out that using an underscore in your code could create conflicts with Cocoa if Apple decide to change their headers and maybe you shouldn't. So underscore prefixes have become a "not advised" coding practice.

dejo
Apr 21, 2011, 10:00 AM
...until Apple pointed out that using an underscore in your code could create conflicts with Cocoa if Apple decide to change their headers and maybe you shouldn't. So underscore prefixes have become a "not advised" coding practice.
Where did Apple point this out?

PhoneyDeveloper
Apr 21, 2011, 10:13 AM
I personally don't like the use of the underscore wart but Apple uses it universally to indicate private ivars and private methods. My understanding is that there is little or no danger of your subclass having a name conflict with a base class in the case of a duplicate ivar. The compiler will catch it. I typically use m for my wart.

In the case of a duplicate method name it is a problem. But it's uncommon for third party developers to use the underscore wart for method names.

webznz
Apr 25, 2011, 06:10 PM
Okay so I have found a nice chapter in my book that I am reading (iPhone programming - the big nerd ranch guide) and chapter 12 has a great section that explains how UINavigation and UIViewController work together, Not only that but it managed to explain how the hierarchy works when dealing with viewControllers. But where I start to get a little bit lost is when I try to create my own object that handles the connection security of my app.

I will show you what I am doing, then explain to you why.. and hopefully then you can help me out with some suggestions.

AppDelegate: As soon as my app loads it calls application:didFinishLaunchingWithOptions: in which I call a method (startPingConnect) from my NSObject pingConnection that I have created and will handle most/all of my connection methods.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

//Cookie check as soon as app loads
pingConnection *Ping = [[pingConnection alloc] init];
[Ping startPingConnect];

// Add the navigation controller's view to the window and display.
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];

return YES;
}



So then When the application starts up and pingConnection is called it executes the code thats in my startPingConnect method below is my pingConnection object.

#import "pingConnection.h"
#import "instaCode1_3AppDelegate.h"
#import "alerts.h" //alertViews are held in this file.


@implementation pingConnection

-(void)startPingConnect{

//urlstart string
NSString *startURL = (@"IPADDRESS_HERE_func=ping");

NSURL *url = [NSURL URLWithString:startURL];

//create a request object with that url
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

//clear out the exisiting connection if there is one
if (connectionInProgress) {
[connectionInProgress cancel];
[connectionInProgress release];
}

//Instantiate the object to hold all incoming data
[cookieData release];
cookieData = [[NSMutableData alloc]init];


//create and initiate the connection - non-blocking
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[cookieData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//we are just checking to make sure we are getting the SML
NSString *cookieCheck = [[[NSString alloc] initWithData:cookieData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"yum yum feed me more cookie!! %@",cookieCheck);

//Put cookie check stuff here..

//CODE=1
if([cookieCheck rangeOfString:@"Code=1"].location != NSNotFound) {
//found it Send error prompt (OK loads new view for fucnt Reg)
NSLog(@"cookieCode = code1");

//Call pleaseRegisterDevice method from alerts object
alerts *newAlert = [[alerts alloc] init];
[newAlert pleaseRegisterDevice];
}else {
//cant find appropriate response...
NSLog(@"cookieCode = code?");
}

}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {

NSLog(@"msg from alertView method");
//open new window


}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connectionInProgress release];
connectionInProgress = nil;

[cookieData release];
cookieData = nil;

NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:errorString delegate:nil cancelButtonTitle:@"OK" destructiveButtonTitle:nil otherButtonTitles:nil];
//[actionSheet showInView:instaCode1_3AppDelegate.window];
[actionSheet autorelease];
}


@end

This code is not funny finished yes as I have to write if statements for each code response but so far it runs through and checks to see if the phone is registered with a cookie number no cookie number or out of sync will give back code 1 which is then sent off to my alert NSObject. which is below

#import "alerts.h"
//////.h file

#import <Foundation/Foundation.h>


@interface alerts : NSObject {

RegisterDeviceViewController *regViewController;

}

@property (nonatomic, retain) IBOutlet RegisterDeviceViewController *regViewController;

- (void)pleaseRegisterDevice;

@end


/////.m file
#import "instaCode1_3AppDelegate.h"
#import "RegisterDeviceViewController.h"


@implementation alerts

//use this alert when phone falls out of sync
- (void)pleaseRegisterDevice {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Register Device"
message:@"click OK to register"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[alert autorelease];
[alert show];

}


//Catch pleaseRegisterDevice method
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"OK"]) {

NSLog(@"msg from alertView method");
//open new wndow
//do I need to create and instance of RegisterDeviceViewController
if (!regViewController) {
regViewController = [[RegisterDeviceViewController alloc] init];
}

//push it onto the top pf the navigation controller's stack
[[self navigationController] pushViewController:regViewController animated:YES];


}
else {
NSLog(@"was not able to push view");
}

}


@end

now I'm having several errors in this code where I'm trying to initialize my view controllers inside alertView:didDismissWithButtonIndex: I'm just wondering if its because your not meant to do this sort of thing inside an NSObject... which I think is the problem and if so how do I go about this while keeping or at least trying to keep the portability of my code (i.e. encapsulated objects)

any help or suggestions would be greatly appreciated I try to learn and listen to everything said to me and am already hugely grateful for the help I have received so far.

EDIT:

opps, so I realized I was declaring my viewController in the wrong header. I have put that in the right header, but now I am still getting a problem where my 'alerts' object may not respond to my navigationController. any idea why?

Shawnpk
Apr 25, 2011, 10:27 PM
Where did Apple point this out?

Sorry for the late reply dejo. I lost the thread. I was looking for where I read my quote about Apple and I couldn't find it. I remember reading about not using underscores somewhere, but with reading so many forums and blogs, I can't track it down, so I guess using them is ok.

webznz
Apr 26, 2011, 12:06 AM
so do you know why I might be getting this error 'NSObject' may not respond to -navigationController ... I'm at a loss.

EDIT:

I found help on stack overflow

You need to get a pointer to your app delegate's navigation controller like so

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController pushViewController:regViewController animated:YES];

I had a feeling that was the problem... but because of my inexperience didn't know what to look for.