按照Scott Sherwood的本教程(http://www.scott-sherwood.com/ios-5-creating-a-custom-side-tabbar-using-storyboards-and-custom-segues/),我创建了一个自定义标签栏,使用子视图加载到其他视图中。一切都很美妙,我对结果非常满意。但是,当我将一个表格视图添加到其中一个“标签页”并将其连接到数据源时,该应用程序正在崩溃。
我正在使用ARC并打开了僵尸,所以我的控制台告诉我已经将消息发送到我的表视图的解除分配的实例。我无法弄清楚如何保留表视图,以便显示我的数据。这只是我的第二个应用程序,我对故事板和高级视图控制器还不太熟悉。我错过了什么?
这是我的自定义标签视图控制器:
CustomTabBarViewController.h
@interface CustomTabBarViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
}
@property(weak,nonatomic)UIViewController *currentViewController;
@property(weak,nonatomic)IBOutlet UIView *placeholder;
@property (weak, nonatomic) IBOutlet UIImageView *tabIndicator;
@property (weak, nonatomic) IBOutlet UIImageView *headerBacker1;
@property (weak, nonatomic) IBOutlet UIImageView *headerBacker2;
@end
CustomTabBarViewController.m
#import "CustomTabBarViewController.h"
@interface CustomTabBarViewController ()
@property (weak, nonatomic) IBOutlet UIView *buttons;
@end
@implementation CustomTabBarViewController
@synthesize currentViewController;
@synthesize placeholder;
@synthesize buttons;
@synthesize tabIndicator;
@synthesize headerBacker1;
@synthesize headerBacker2;
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSegueWithIdentifier:@"ProductSegue" sender:[self.buttons.subviews objectAtIndex:0]];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setTabIndicator:nil];
[self setHeaderBacker1:nil];
[self setHeaderBacker2:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"ProductSegue"]
|| [segue.identifier isEqualToString:@"ContactSegue"]
|| [segue.identifier isEqualToString:@"CategoryPage"]){
if([segue.identifier isEqualToString:@"ContactSegue"]){
[UIView animateWithDuration:0.25f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
[tabIndicator setCenter:CGPointMake(614, 108)];
[headerBacker1 setCenter:CGPointMake(1024, 48)];
[headerBacker2 setCenter:CGPointMake(1024, 789)];
} completion:nil];
}else{
[UIView animateWithDuration:0.25f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
[tabIndicator setCenter:CGPointMake(499, 108)];
[headerBacker1 setCenter:CGPointMake(341, 48)];
[headerBacker2 setCenter:CGPointMake(341, 789)];
} completion:nil];
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
@end
加载到选项卡中的页面,其中包含表视图: (注意*:此页面链接为故事板中的tableview数据源和表视图委托)
ProductListViewController.h
#import <UIKit/UIKit.h>
@interface ProductListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSMutableArray *maTheData;
@end
ProductListViewController.m
#import "ProductListViewController.h"
@interface ProductListViewController ()
@end
@implementation ProductListViewController
@synthesize maTheData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad");
if (!maTheData) {
maTheData = [[NSMutableArray alloc] initWithObjects:@"Comets", @"Asteroids", @"Moons", nil];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [maTheData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"categoryTableCell"];
UILabel *lblName = (UILabel *)[cell viewWithTag:100];
[lblName setText:[maTheData objectAtIndex:[indexPath row]]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//return UIInterfaceOrientationIsLandscape(interfaceOrientation);
return interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
@end
感谢您的想法。
答案 0 :(得分:0)
我猜你遇到的问题是因为你有弱属性而不是强属性,特别是对于你的currentViewController。尝试将它们全部更改为强大,看看你是否还有问题。
取自:Weak and strong property setter attributes in Objective-C
弱(iOS4 = unsafe_unretained)
答案 1 :(得分:0)
@property (nonatomic, strong) IBOutlet UITableView *tableView;
将此属性与故事板中的tableview相关联。这应该会阻止tableview进行处理,因为视图控制器有一个强指针。从调试点开始在您认为导致错误的区域中放置断点以找到代码断开的行。然后,您可以更深入地调查错误。我希望这有帮助。
答案 2 :(得分:0)
据我所知,从你提供的链接中可以看出,你想要的控制器应该由currentViewController指向。所以,问题在于这个宣言:
@property(弱,非原子)UIViewController * currentViewController;
这需要强大而不是弱。 IBOutlets很容易变弱,因为指向的对象是另一个视图的子视图,它对它们有强烈的引用。但是,currentViewController应该很强大,以防止你的控制器被释放(没有其他任何东西有强烈的引用)。我从链接中看到他在current方法中将currentViewController设置为segue的目标控制器 - 如果你这样做,并且你将声明更改为strong,那么你应该没问题。