正在处理带有选择器的简单实用程序应用程序,但邮件输出不正确。第二个组件没有正确地实例化,而是输出“我正在睡觉并且感觉InstatwitViewController关于它。”
而不是输出“我正在睡觉并对此感到高兴。”。IB中已经连接了所有内容。
你能告诉我什么错吗?
这是.h文件:
#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
这是实施文件:
#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",@"confused", @"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
答案 0 :(得分:1)
如果我们修复了缩进,您的sendButtonTapped:
看起来像这样:
- (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);
}
换句话说,你的括号被误导,你永远不会将“感觉”传递给格式字符串。而且你“幸运”它并没有让你崩溃。