【问题标题】:Why is my tab bar controller crashing?为什么我的标签栏控制器崩溃了?
【发布时间】:2011-04-05 17:48:17
【问题描述】:

我正在尝试创建一个使用标签栏控制器的 iPhone 应用程序。第一个选项卡工作正常。

但是,当我单击选项卡栏中的第二个选项卡时,整个应用程序崩溃了。我正在尝试在第二个选项卡中实现表格视图。什么可能导致崩溃?

这是我的代码:

SecondViewController.h

#import <UIKit/UIKit.h>

@class Person;

@interface SecondViewController : UIViewController<UITableViewDelegate, UITableViewDataSource >{
    UITableView *tableView;
    NSArray *persons;

}
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic,retain ) NSArray *persons;
-(void)initPersons:(NSArray *) array;

@end

SecondViewController.m

#import "SecondViewController.h"
#import "Person.h"


@implementation SecondViewController

@synthesize tableView;
@synthesize persons;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

- (id)init {

    if (self = [super initWithNibName:@"SecondViewController" bundle:nil]) {

        //self.title = @"Slot";



        UIImage* anImage = [UIImage imageNamed:@"cherry.png"];

        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"table" image:anImage tag:0];

        self.tabBarItem = theItem;

        [theItem release];

    }

    return self;

}

-(void)initPersons:(NSArray *) array{
    int size = [array count];
    int i = 0;
    NSMutableArray *aPersons = [NSMutableArray array];
    while (i < size) {
        Person *person = [[Person alloc]init];
        NSString * name =[array objectAtIndex:i];
        NSArray *chunks =[name componentsSeparatedByString: @" "];
        person.firstName = [chunks objectAtIndex:0];
        person.lastName = [chunks objectAtIndex:[chunks count]-1];
        [aPersons addObject:person];

        [person release];
        i++;
    }
    self.persons=aPersons;
    [aPersons release];

}

-(NSArray *)sortArray {
    NSSortDescriptor *lastNameDescriptor = [[[NSSortDescriptor alloc]

                           initWithKey:@"lastName"

                           ascending:YES

                           selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];

    NSSortDescriptor *firstNameDescriptor = [[[NSSortDescriptor alloc]

                            initWithKey:@"firstName"

                            ascending:YES

                            selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];

    NSArray *sortDescriptors = [NSArray arrayWithObjects:lastNameDescriptor,

                       firstNameDescriptor, nil];

    return [persons sortedArrayUsingDescriptors:sortDescriptors];
}   


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    NSArray *array = [[NSArray alloc] initWithObjects:
                      @"Amin Alrusayni", @"Berksan Ates",
                      @"Becca Bedell", @"Joseph Carioti",
                      @"Christopher Conry", @"Jeremy Dobbins", @"Timothy Fox",
                      @"Eric Green", @"Timothy Gruscinski", @"Daniel Gur", 
                      @"Yousef Guzaiz", @"Tyler Herzog", @"Alicia Johnson", @"Scott Kazakis",
                      @"Nathan Kidwell", @"Dakota Kincer", @"Scott Moore",
                      @"Sean Reber", @"Michael Romeo", @"Alexander Shibble",
                      @"Joshua Shipley", @"Mitchell Slemc", @"Thomas Smith",
                      @"Christopher Wagner", @"Corey Zachrich", @"Ahmed Alalawi",
                      @"Abdullah Alqahtani", @"Daniel Angelis", @"Brian Bartman",
                      @"William Haverstock", @"Hui Hong", @"Rong Li",
                      @"Amitkumar Mali", @"Christian Newman", nil]; 

    [self initPersons:array];
    NSArray *sortedArray = [self sortArray];
    for (Person *person in sortedArray)
    {
        NSString *fullName = [[person.firstName stringByAppendingString:@" "] stringByAppendingString:person.lastName];
        NSLog(@"%@",fullName);
        NSLog(@" ");
    }
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
//commented out this function
/*
- (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 {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [tableView dealloc];
    [super dealloc];
}

#pragma mark -
#pragma mark TableView DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.persons count];
    }

/*- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{






    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:SimpleTableIdentifier] autorelease];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [persons objectAtIndex:row];
    return cell;

}*/
@end

【问题讨论】:

  • 控制台或崩溃日志在崩溃时会说什么?
  • 欢迎来到 StackOverflow!请注意格式和拼写等事项。如果你这样做,人们会很乐意帮助你。另外,我建议您阅读the FAQ 以帮助您入门。也就是说,您遇到了什么错误?

标签: objective-c uitabbarcontroller


【解决方案1】:

如果您按照 Moshe 的评论使用降价格式发布代码,会更容易提供帮助。通过快速扫描,我确实注意到了一些事情。我也无法从中看出您在界面构建器中创建了什么,以及 UITabBarController 和所有插座是否已正确配置。除了以下内容之外,您可能还有其他事情要做,但这是一个开始。

  1. 确保释放您保留的任何内容。例如,在 viewDidLoad 中分配 array 并且永远不会释放它。

  2. 同样,不要发布你没有保留的东西。在initPersons 中,您使用返回自动释放对象的数组构造函数创建可变数组aPersons。然后你有[aPersons release]。这将导致崩溃 b/c 你正在释放一个你没有保留的对象。

  3. viewDidUnloaddealloc 中正确清理。在这两种情况下,您都需要释放tableView。在dealloc 你有[tableView dealloc]。那应该是[tableView release]

  4. 您的数据源创建过于复杂,但我确实看到了一个明显的问题。您在initPersons 中设置self.persons,这很好。然后对数组进行排序并将其存储在sortedArray 中。假设您的意图是保持数组排序,那么您当前正在丢弃已排序的数组。 self.persons 仍然是未排序的版本。虽然这不是崩溃,但我怀疑这是您的意图。

  5. 你需要实现tableView:cellForRowAtIndexPath:这如果丢失绝对会导致崩溃。这是假设您正确配置了 tableView 委托和数据源。您应该将协议UITableViewDataSourceUITableViewDelegate 添加到SecondViewController 的接口定义中,这样您将获得有关实现所需协议方法的正确编译器警告。

【讨论】:

    【解决方案2】:

    – tableView:cellForRowAtIndexPath: 是 UITableviewDatasource 协议的必需方法。所以启用它。开心

    如果你不想要任何实现,那么就留下空白定义。

    { 返回单元格; }

    【讨论】:

    • 这是不正确的。 tableView:cellForRowAtIndexPath: 必须返回一个有效的 UITableViewCell 实例。
    猜你喜欢
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多