![](/img/trans.png)
[英]xcode: following tutorial but 'subViewOneController' undeclared error
[英]In Xcode an error says 'activities' undeclared
您好,我是開發新手,我想知道你們中的任何一個專業人士是否知道如何解決這個問題。 我的代碼如下:InstaTwitViewController.m:
#import "InstaTwitViewController.h"
@implementation InstaTwitViewController
/*
// 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 {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super 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];
}
*/
/*
// 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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[activities release];
[feelings release];
[super dealloc];
}
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)
pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)
pickerViewnumberOfRowsInComponent :(NSInteger)component {
if (component == 0) {
return [activities count];
}
else {
return [feelings count];
}
}
@end
在 [activities count] 和 [activities release] 旁邊,它指出錯誤“'activities' undeclared”
InstaTwitViewController.h:
//
// InstaTwitViewController.h
// InstaTwit
//
// Created by John Bridge on 5/2/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface InstaTwitViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
NSArray* actvities;
NSArray* feelings;
}
@end
編輯
您的財產聲明中有錯字。
actvities
應該是activities
,帶有i
。
在編碼和閱讀自己的代碼時,您應該更加小心......
編輯結束
顯然,您還沒有聲明activities
變量。 這就是為什么 XCode 說它是未申報的……
我想它應該是一個 NSArray... 您需要在 class 接口(header 文件)中聲明變量。
就像是:
@interface InstaTwitViewController: UIViewController
{
NSArray * activities;
}
@end
然后,在您的實現中,您需要分配它,例如在 init 方法中:
- ( id )initWithNibName: ( NSString * )nibNameOrNil bundle: ( NSBundle * )nibBundleOrNil
{
if( ( self = [ super initWithNibName: nibNameOrNil bundle: nibBundleOrNil ] ) )
{
activities = [ NSArray new ];
}
return self;
}
並且不要忘記在 dealloc 方法中釋放它:
- ( void )dealloc
{
[ activities release ];
[ super dealloc ];
}
去掉 viewDidLoad 方法前后的/*
和*/
代碼,並在 your.h 文件中聲明一個名為 activity 的 NSArray。
NSArray *activities;
編輯----正如麥克梅德所說,只需修復拼寫錯誤!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.