What Am I Doing Wrong

Discussion in 'iOS Programming' started by HyperSnake, Mar 18, 2009.

  1. HyperSnake macrumors member

    Joined:
    Jan 5, 2009
    Location:
    Switzerland
    #1
    I'm trying to make a simple joke app but when I launch the app in the simulator, nothing comes up in the UITextField. Can any of you experienced iPhone programmers please help me?

    Here is my .h file :
    Code:
    #import <UIKit/UIKit.h>
    
    @interface iVittighederViewController : UIViewController {
    	IBOutlet UITextView	  *jokeTextView;
    			 NSDictionary *jokeDictionary;
    			 NSString	  *lastJoke;
    }
    
    @property (retain, nonatomic) UITextView	*jokeTextView;
    @property (retain, nonatomic) NSDictionary  *jokeDictionary;
    @property (retain, nonatomic) NSString		*lastJoke;
    
    - (IBAction)buttonPressed:(id)sender;
    
    @end
    
    And here is my .m file. I have yet to write the buttonPressed method but since it will be almost identical to viewDidLoad I don't suppose it'll be a problem.

    Code:
    #import "iVittighederViewController.h"
    
    @implementation iVittighederViewController
    @synthesize jokeTextView;
    @synthesize jokeDictionary;
    @synthesize lastJoke;
    
    - (IBAction)buttonPressed:(id)sender 
    {
    	
    }
    
    
    - (void)viewDidLoad 
    {
    	NSBundle *bundle = [NSBundle mainBundle];
    	NSString *vittighederPlistPath = [bundle pathForResource:@"vittigheder" ofType:@"plist"];
    	
    	NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:vittighederPlistPath];
    	self.jokeDictionary = dictionary;
    	[dictionary release];
    	
    	NSArray *array = [self.jokeDictionary allKeys];
    	
    	int number = random() % [array count];
    	NSString *theString = [[NSString alloc] initWithFormat:@"vittighed%i", number];
    	NSString *output = [jokeDictionary valueForKey:theString];
    	jokeTextView.text = output;
    	
    	lastJoke = theString;
    	
    	srandom(time(NULL));
    	
    	[theString release]; 
    	[super viewDidLoad];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; 
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    
    Here is a picture of the .plist I'm using:
    [​IMG]

    I don't suppose screenshots of my IB project is need, it's just a background, a UITextView and a button and everything is linked, but if you do, I can add some screenshots.

    P.S. It is the first time I'm dealing with the UITextView class, but I don't suppose I need to implement any protocols if all I use it for is displaying text???

    P.P.S. This app is in danish. "vittighed" means joke and "vittigheder" means jokes. Just thought you'dd like to know

    - Ben
     

    Attached Files:

  2. Taum macrumors member

    Joined:
    Jul 28, 2008
    #2
    You should use the integrated debugger to help you debug the code. Just make sure you use the "Debug" configuration to avoid some surprises ;)
    You can click on the left margin to set a breakpoint in the editor and the step through the code and inspect variables.

    Code:
    NSString *theString = [[NSString alloc] initWithFormat:@"vittighed%i", number];
    You might have a problem here because you used a lowercase 'v' vs. uppercase 'V' in the plist file.

    BTW, your plist should really be an Array and not a Dictionary if you don't really use the keys for anything interesting. Rather that forging the keyname you would simply use objectAtIndex:
     
  3. HyperSnake thread starter macrumors member

    Joined:
    Jan 5, 2009
    Location:
    Switzerland
    #3
    Thank you Im gonna try that out later.

    It is the first time
    I'm dealing with plists so thank you very much for the advice. Now that you mention it, it makes perfect sense to me.
     

Share This Page