开发者

Why is my tab bar controller crashing?

开发者 https://www.devze.com 2023-02-22 11:46 出处:网络
I\'m trying to create a iPhone app that uses a tab bar controller. The first tab works fine. However, when I click the second tab in the tab bar the whole app crashes. I am trying to implement a tab

I'm trying to create a iPhone app that uses a tab bar controller. The first tab works fine.

However, when I click the second tab in the tab bar the whole app crashes. I am trying to implement a table view in the second tab.What could be causing the crash?

Here is my code:

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",
                      @"A开发者_开发技巧bdullah 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


It would be easier to help if you post your code using markdown formatting as Moshe commented. I did notice a few things with a quick scan. I also can't tell from this what you have created in interface builder and if the UITabBarController and all outlets are properly configured. You may have other things going on besides the following but here's a start.

  1. Make sure you release anything you retain. for example, in viewDidLoad you allocate array and never release it.

  2. Similarly, don't release things you haven't retained. In initPersons you create the mutable array aPersons using an array constructor that returns an autoreleased object. You then have [aPersons release]. This will cause a crash b/c you are releasing an object you haven't retained.

  3. Clean up properly in viewDidUnload and dealloc. In both of these you need to release tableView. In dealloc you have [tableView dealloc]. That should be [tableView release]

  4. Your data source creation is overly complicated but I do see a clear problem. You are setting self.persons in initPersons which is fine. You then sort the array and store it in sortedArray. Assuming your intent is to keep the array sorted, you are currently discarding the sorted array. self.persons remains the unsorted version. While this is not a crash, I doubt this was your intent.

  5. You need to implement tableView:cellForRowAtIndexPath: This will absolutely cause a crash if missing. That's assuming you have the tableView delegate and dataSource configured correctly. You should add the protocols UITableViewDataSource and UITableViewDelegate to the interface definition for SecondViewController so you'll get the proper compiler warnings regarding implementing required protocol methods.


– tableView:cellForRowAtIndexPath: is required method of UITableviewDatasource protocol.so enable it.be happy

if u dont want any implementaion in it then just leave it with blank definition.

{ return cell; }

0

精彩评论

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

关注公众号