选择器中的第二个组件未正确实例化

发布于 2024-10-29 08:17:03 字数 3341 浏览 3 评论 0原文

正在使用选择器开发简单的实用程序应用程序,但消息未正确输出。第二个组件没有输出“我正在睡觉并且对此感到高兴”之类的内容,而是没有正确实例化,而是输出“我正在睡觉并且对此感到 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

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",@"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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2024-11-05 08:17:04

如果我们修复缩进,您的 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);
}

换句话说,您的方括号嵌套错误,导致您永远无法将“感觉”传递给格式字符串。你很“幸运”,它没有撞到你身上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文