Resolved Help please, trying to get this view to work

Discussion in 'iPhone/iPad Programming' started by troop231, Mar 30, 2011.

  1. troop231, Mar 30, 2011
    Last edited: Apr 4, 2011

    macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #1
    Hi, I have a simple questions view I need to load when a user taps on the selected row in the previous root view, but my view that gets loaded is just blank:
    [​IMG]

    When it should look like this with the nav bar at the top:

    [​IMG]

    Here is my code: Questions.h:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface Questions :   UIViewController
    {   UIView *View;
    	
    	int currentQuestionIndex;
    	
    	// The model objects
    	NSMutableArray *questions;
    	
    	
    	//The view objects
    	IBOutlet UILabel *questionField;
    	
    	
        
    }
    
    @property (nonatomic, retain) UIView *view;
    
    - (IBAction)showQuestion:(id)sender;
    
    
    @end
    and Questions.m:

    Code:
    #import "Questions.h"
    
    
    @implementation Questions
    
    @synthesize view;
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {
    	return (interfaceOrientation !=
    			UIInterfaceOrientationPortraitUpsideDown);
    }
    
    - (void)viewDidLoad { 
    	
    	[super viewDidLoad];
    	self.title = @"Test"; }
    
    -(id)init
    {
    	// Call the init method implemented by the superclass
    	[super init];
    	
    	// Create two arrays and make the pointers point to them
    	questions = [[NSMutableArray alloc] init];
    	
    	
    	// Add questions and answers to the arrays
    	[questions addObject:@"Where do you think our country lacks?"];
    	
    	
    	[questions addObject:@"Who do you feel is the most popular person in the world? Why?"];
    	
    	
    	[questions addObject:@"Should the government bail out failing companies?"];
    	
    	
    	
    	
    	
    	// Return the address of the new object
    	return self;
    	
    }
    
    - (IBAction)showQuestion:(id)sender
    {
    	// Step to the next question - just to keep things simple
    	// to focus on the iOS elements of the programming,
    	// we will start with the "second" question in the list.
    	currentQuestionIndex++;
    	
    	// Am I past the last question?
    	if (currentQuestionIndex == [questions count]) {
    		// Go back to the first question
    		currentQuestionIndex = 0;
    	}
    	
    	// Get the string at that index in the questions array
    	NSString *question = [questions objectAtIndex:currentQuestionIndex];
    	
    	// Log the string to the console
    	NSLog(@"displaying question: %@", question);
    	
    	//Display the string in the question field
    	[questionField setText:question];
    	
    	
    	
    }
    
    
    
    - (void)dealloc {
    	[view release];
        [super dealloc];
    }
    
    
    @end
    I know the answer will seem very easy to someone here, so any help is appreciated! I need to get this working by Friday :(
     
  2. macrumors member

    ritsard

    Joined:
    Jun 18, 2009
    Location:
    SF Bay Area, CA
    #2
    How are you loading the Questions view in your app? Is it declared and used in the App delegate?
     
  3. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #3
    In my root view controller:

    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
         //Get the dictionary of the selected data source.
         NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
         
         //Get the children of the present item.
         NSArray *Children = [dictionary objectForKey:@"Children"];
         
         if([Children count] == 0) {
              NSInteger ViewNumber = [[dictionary objectForKey:@"View"] integerValue];
              switch (ViewNumber) {
                   case 1: {
                        RootViewController *rvc = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
                        //Switch the view here
                        rvc.view = tbController.view;
                        [self.navigationController pushViewController:rvc animated:YES];
                        [rvc release];
                        }
                        break;
                   case 2: {
                         Questions *ivc2 = [[Questions alloc] initWithNibName:@"Questions" bundle:[NSBundle mainBundle]];
                        
                        [self.navigationController pushViewController:ivc2 animated:YES]; 
                        [ivc2 release];
                        }
                        break;
    
    
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    #4
    Post any warnings or errors that appeared during compilation.
    Don't paraphrase them, copy and paste them exactly.


    Code:
    @interface Questions :   UIViewController
    {   UIView *[COLOR="Red"]View[/COLOR];
    ...
    @property (nonatomic, retain) UIView *[COLOR="red"]view[/COLOR];
    
    The red-hilited code looks wrong to me.

    The instance variable's name is View (capital V), but your property name is view (small v). Objective-C is case-sensitive, so View and view are different things.

    Furthermore, UIViewController already has a view property. It seems like a bad idea to me to override it with your own synthesized property of the same name. It seems like an equally bad idea to have a second property or ivar named View, which seems to have the same purpose as the existing view property.
     
  5. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #5
    There isn't any compilation errors, the app runs fine, but as I showed above with the white pic that is all that gets loaded, when the blue should be shown instead, and I did fix the capital 'V' in view so disregard that.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Why do you think you need to override UIViewController's view property?
     
  7. troop231, Mar 30, 2011
    Last edited by a moderator: Mar 30, 2011

    thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #7
    I didn't think lol I just don't know what to do at all! So what should I do to get this thing to work?

    EDIT:

    I deleted the reference to UIView *view guys and I ALMOST have this solved!

    Now when I push on the "Next Question" button the app crashes! Possibly wrong connections/issues in IB?

    here is a screenshot and the console output:

    [​IMG]

    Code:
    [Session started at 2011-03-30 15:53:39 -0400.]
    2011-03-30 15:53:46.893 PageantPlanet[17248:207] -[__NSCFType showQuestion:]: unrecognized selector sent to instance 0x4c4ca80
    2011-03-30 15:53:46.895 PageantPlanet[17248:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType showQuestion:]: unrecognized selector sent to instance 0x4c4ca80'
    *** Call stack at first throw:
    (
    	0   CoreFoundation                      0x00e24be9 __exceptionPreprocess + 185
    	1   libobjc.A.dylib                     0x00f795c2 objc_exception_throw + 47
    	2   CoreFoundation                      0x00e266fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    	3   CoreFoundation                      0x00d96366 ___forwarding___ + 966
    	4   CoreFoundation                      0x00d95f22 _CF_forwarding_prep_0 + 50
    	5   UIKit                               0x0032da6e -[UIApplication sendAction:to:from:forEvent:] + 119
    	6   UIKit                               0x003bc1b5 -[UIControl sendAction:to:forEvent:] + 67
    	7   UIKit                               0x003be647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    	8   UIKit                               0x003bd1f4 -[UIControl touchesEnded:withEvent:] + 458
    	9   UIKit                               0x003520d1 -[UIWindow _sendTouchesForEvent:] + 567
    	10  UIKit                               0x0033337a -[UIApplication sendEvent:] + 447
    	11  UIKit                               0x00338732 _UIApplicationHandleEvent + 7576
    	12  GraphicsServices                    0x0175aa36 PurpleEventCallback + 1550
    	13  CoreFoundation                      0x00e06064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    	14  CoreFoundation                      0x00d666f7 __CFRunLoopDoSource1 + 215
    	15  CoreFoundation                      0x00d63983 __CFRunLoopRun + 979
    	16  CoreFoundation                      0x00d63240 CFRunLoopRunSpecific + 208
    	17  CoreFoundation                      0x00d63161 CFRunLoopRunInMode + 97
    	18  GraphicsServices                    0x01759268 GSEventRunModal + 217
    	19  GraphicsServices                    0x0175932d GSEventRun + 115
    	20  UIKit                               0x0033c42e UIApplicationMain + 1160
    	21  PageantPlanet                       0x000029c4 main + 102
    	22  PageantPlanet                       0x00002955 start + 53
    )
    terminate called after throwing an instance of 'NSException'
     
  8. macrumors 6502

    Joined:
    Jun 19, 2009
    #8
    Missing Sentinel

    Not sure if it's related to your crash, but here's something I noticed:

    In the init method in your Questions implementation file, you declared an array but you never added a nil value as the last component of the array. I was under the impression an NSArray(or NSMutableArray which is a sub class of NSArray) needs to have a nil as its last component.

    Also, is there a reason you are using an NSMutableArray vs. just NSArray? If you are providing preloaded questions and not allowing the user to add their own questions, then you might ought to use just NSArray. All the books I've been going through use the arrayWithValues (if I remember correctly) method to add values to the array with a nil value being the last. This is done so that when you iterate through the values in the array and you reach nil, then you know you've reached the end of the array.
     
  9. chown33, Mar 30, 2011
    Last edited: Mar 30, 2011

    macrumors 603

    Joined:
    Aug 9, 2009
    #9
    Post your revised code.


    This error message:
    Code:
    2011-03-30 15:53:46.893 PageantPlanet[17248:207] -[__NSCFType showQuestion:]: unrecognized selector sent to instance 0x4c4ca80
    
    strongly suggests a message is being sent to an object that isn't of the class it needs to be. The message being sent is showQuestion:. That should ring a bell, and tell you something about which object is the wrong type. It should also suggest something about which connection or which instance in the nib is incorrect.

    I get the impression you're rushing through this without a clear software design. Consequently, you're making mistakes that would be avoidable with a clear design or even a simple roadmap. That means you have to go back and fix dumb mistakes that could be avoided with just a little planning and forethought, which takes longer overall than if you'd simply put in the planning in the first place. Haste makes waste.



    This impression is incorrect.

    The nil terminator is used only with certain methods, which initialize the array from a variable number of objects. When using those methods, the nil terminator is not added to the array.

    No nil is needed in the OP's code because it isn't using the initializer which takes a variable number of objects
     
  10. troop231, Mar 30, 2011
    Last edited: Mar 30, 2011

    thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #10
    Here is revised code: Questions.h:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface Questions :   UIViewController
    {   
    	
    	int currentQuestionIndex;
    	
    	// The model objects
    	NSMutableArray *questions;
    	
    	
    	//The view objects
    	IBOutlet UILabel *questionField;
    	
    	
        
    }
    
    @property (nonatomic, retain) UIView *view;
    
    - (IBAction)showQuestion:(id)sender;
    
    
    @end
    and Questions.m:

    Code:
    #import "Questions.h"
    
    
    @implementation Questions
    
    
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation {
    	return (interfaceOrientation !=
    			UIInterfaceOrientationPortraitUpsideDown);
    }
    
    - (void)viewDidLoad { 
    	
    	[super viewDidLoad];
    	self.title = @"Interview Questions"; }
    
    -(id)init
    {
    	// Call the init method implemented by the superclass
    	[super init];
    	
    	// Create two arrays and make the pointers point to them
    	questions = [[NSMutableArray alloc] init];
    	
    	
    	// Add questions and answers to the arrays
    	[questions addObject:@"Where do you think our country lacks?"];
    	
    	
    	[questions addObject:@"Who do you feel is the most popular person in the world? Why?"];
    	
    	
    	[questions addObject:@"Should the government bail out failing companies?"];
    	
    	
    	// Return the address of the new object
    	return self;
    	
    }
    
    - (IBAction)showQuestion:(id)sender
    {
    	// Step to the next question - just to keep things simple
    	// to focus on the iOS elements of the programming,
    	// we will start with the "second" question in the list.
    	currentQuestionIndex++;
    	
    	// Am I past the last question?
    	if (currentQuestionIndex == [questions count]) {
    		// Go back to the first question
    		currentQuestionIndex = 0;
    	}
    	
    	// Get the string at that index in the questions array
    	NSString *question = [questions objectAtIndex:currentQuestionIndex];
    	
    	// Log the string to the console
    	NSLog(@"displaying question: %@", question);
    	
    	//Display the string in the question field
    	[questionField setText:question];
    	
    	
    	
    }
    
    
    
    - (void)dealloc {
    	
        [super dealloc];
    }
    
    
    @end
    Here are the screenshots of the IB connections:

    Overview: [​IMG]

    Button: [​IMG]
    Files Owner: [​IMG]
    Label: [​IMG]
    Questions: [​IMG]
    View: [​IMG]
     
  11. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #11
    Another thing is, u could also do bundle:nil when ur pushing ur view.
    U also can remove the property of the UIView, since it's a lose reference.
    Might be that's that the problem to for the crash.
    Be carefull with overriding the standard stuff :)
    Good luck
     
  12. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #12
    It's not crashing anymore when the view is loaded, only when I push on the button: "Next Question"
     
  13. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #13
    Do what i said anyways, and try commenting out allt he lines inside your method, see what that does. then line per line add back in, see where it crashes, or just use the debugger.
     
  14. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    Are you sure your Question's init method is being called? Because you are using initWithNibName:bundle: in your rootViewController:
     
  15. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #15
    Yeah, pretty sure, so far today I've made it to where it doesn't crash when you push "Next Question" However the UILabel is blank when I push it, so I'm not sure where my connections are wrong? Screenshots:

    Xib File Owner Connections:

    [​IMG]


    Questions Xib loaded: [​IMG]


    When Next Question button is pushed Label is blank: [​IMG]
     
  16. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Pretty sure? I would make absolutely sure. Put a breakpoint in your init, say where you add your first object to questions. When you run the code, does it stop at the breakpoint? Just so you know, I've tried to reproduce your project and my breakpoint doesn't get reached.
     
  17. troop231, Mar 31, 2011
    Last edited: Mar 31, 2011

    thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #17
    I know it's working, the Questions.xib is being loaded when case 2 is being called from my plist. I did the same thing with my outdoor app.

    I just still don't know where the connections are wrong.

    Edit: I don't know how to do a breakpoint anyways.
     
  18. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #18
    Well, it's not really working, is it? Sure, your view is being presented but your questions array is not being loaded with objects.

    Well, then, now would be a good opportunity to learn how, wouldn't you say? I'd suggest checking the Xcode User Guide for more information.
     
  19. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #19
    I'm even more lost now, I searched within the iOS dev library on Apple's site and it just confused me even more, I just want to know why this isn't working when It worked fine as a stand alone app that was in my Apress book.
     
  20. macrumors 603

    Joined:
    Aug 9, 2009
    #20
    What else could you do that would confirm that a certain point in the code is being reached? Maybe some kind of log that you could write messages to, like "In init method of BarkingMad class".

    And at this point, most of Apple's site has probably been changed to Xcode 4 docs, so searching there is unlikely to help if you're using Xcode 3. Maybe there's some kind of builtin help in Xcode, perhaps even a menu named Help or something where you can type in the search term "breakpoint" or "debugger".

    It doesn't work because something is different. Exactly what, only you can determine, because you're currently the only one with all the parts of the project. If only there were some way for others to get access to all the project parts, they might be able to figure it out.

    If you didn't start with a known-working project, then you may not have done everything needed to make things work. You could start over with a known-working project, then modify it gradually, making sure it still works after every change, no matter how small. Honestly, that might be the simplest and quickest route, given that your other alternative is starting from scratch on learning how to use the debugger, then becoming proficient enough to actually use the debugger to find the problem.
     
  21. troop231, Mar 31, 2011
    Last edited: Mar 31, 2011

    thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #21
    Very true, I tried Xcode 4 and hated it because none of the books talk about how to use it yet. Also the app works perfectly as a stand alone app from the book, so I copied the source into a new source file subtracting the app delegate bits since I want this to be an addition to my app and everything is sour, although I feel like I'm one step away because It's not crashing at all, it's just when the Next Question button is pressed, nothing is displayed in the label is my only issue right now :(

    EDIT: Here is a link to the working stand alone app project from my book (19KB zip) http://www.2shared.com/file/cTATQbzA/Quiz.html
     
  22. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #22
    I believe you're close too. Have you done any further debugging? You don't necessarily need to put in breakpoints; you could just add some NSLog() statements (as chown33 alluded to) to help verify that what you expect to happen is actually happening. I just thought that now would be a good opportunity to become comfortable with some of the Xcode debugging tools at your disposal.
     
  23. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #23
    Mr. dejo, did you see the included zip file I linked above? Also, no I haven't tried any other debugging because really I don't know what to debug if it works. NSLog() confuses me too.
     
  24. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #24
    Yes, I did. I downloaded it and tried it; it works fine! It's setup a little strange in that there is no viewController in the app; most of the work is done in the app delegate. But when I ran it, I had non-null text showing up in the label when pressing the Next Question button. So, I'm not sure what changes you have made between this working app and your not-working one. Would you be willing to attach a zip of your project?

    I'm confused. Are you still claiming your app works? Since your label is showing blanks that, to me, means isn't not working. Unless you meant something else.

    NSLog() is pretty simple, especially if you use it to just print a simple string to your debugger console (for example, NSLog(@"Hello World"); ). If you can't tackle that, I'm not sure what I can tell you.
     
  25. thread starter macrumors 603

    troop231

    Joined:
    Jan 20, 2010
    #25
    Great dejo, I really didn't change much at all, since that was a standalone app I needed to port over the code and make it a non delegate source file (if that makes any sense) while leaving the bulk of the code intact.

    I can include a zip of my actual project, I will PM you the link.
     

Share This Page