开发者

Second component in Picker not instantiating correctly

Am working on simple utility app with picker, but the message does not output correctly. Instead of outputting something like "I'm sleeping and feeling happy about it.", the second component doesn't instantiate correctly, and it instead outputs "I'm sleeping and feeling InstatwitViewController about it."

Everything has been connected in IB.

Can you tell me what wrong?

Here is the .h file:

#import <UIKit/UIKit.h>

@interface InstatwitViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate>
{
    IBOutlet UIPickerView *tweetPicker;
    NSArray* activities;
    NSArray* feelings;
}

@property (nonatomic,retain)UIPickerView* tweetPicker; 
- (IBAction) sendButtonTapped:(id)sender;

@end

And here is the implementation file:

#import "InstatwitViewController.h"

@implementation InstatwitViewController
@synthesize tweetPicker;



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
    {
        return 2;
    }

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        if (component==0) {
            return [activities count];
        }
        else
            return [feelings count];
    }





// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    activities = [[NSArray alloc] initWithObjects:@"sleeping", @"eating", @"working", @"thinking", @"crying", @"begging", @"leaving", @"shopping", @"hello worlding", nil];
    feelings = [[NSArray alloc] initWithObjects:@"awesome", @"sad", @"happy", @"ambivalent", @"nauseous", @"psyched",@"conf开发者_StackOverflow中文版used", @"hopeful", @"anxious", nil];
    [super viewDidLoad];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component){
        case 0:
            return [activities objectAtIndex:row];
        case 1:
            return [feelings objectAtIndex:row];
    }
    return nil;
}

- (IBAction) sendButtonTapped:(id)sender
{
    NSString* themessage = [NSString stringWithFormat:@"I'm %@ and feeling %@ about it.",
                            [activities objectAtIndex:[tweetPicker selectedRowInComponent:0]]];
                            [feelings objectAtIndex:[tweetPicker selectedRowInComponent:1]];
    NSLog(themessage);
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [tweetPicker release];
    [activities release];
    [feelings release];
    [super dealloc];
}

@end


If we fix the indentation, your sendButtonTapped: looks like this:

- (IBAction) sendButtonTapped:(id)sender
{
    NSString* themessage = [NSString stringWithFormat:@"I'm %@ and feeling %@ about it.",
                            [activities objectAtIndex:[tweetPicker selectedRowInComponent:0]]];
    [feelings objectAtIndex:[tweetPicker selectedRowInComponent:1]];
    NSLog(themessage);
}

In other words, your brackets are misnested in such a way that you're never passing the "feeling" to the format string. And you're "lucky" it's not crashing on you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜