gpt4 book ai didi

iOS 5 自定义标签栏取消分配 TableView ?

转载 作者:行者123 更新时间:2023-12-01 16:50:43 25 4
gpt4 key购买 nike

按照 Scott Sherwood 的教程 (http://www.scott-sherwood.com/ios-5-creating-a-custom-side-tabbar-using-storyboards-and-custom-segues/),我创建了一个自定义选项卡栏,它使用 subview 加载到其他 View 中。一切都很顺利,我对结果非常满意。但是,当我将表格 View 添加到“标签页”之一并将其连接到数据源时,应用程序崩溃了。

我正在使用 ARC 并打开了僵尸,所以我的控制台告诉我一条消息已发送到我的 TableView 的已释放实例。我不知道如何保留表格 View ,以便显示我的数据。这只是我的第二个应用程序,我对 Storyboard和高级 View Controller 还不太熟悉。我错过了什么?

这是我的自定义选项卡 View Controller :

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

以及加载到选项卡中的页面,其中包含表格 View :
(注*:此页面在storyboard中作为tableview数据源和table view delegate链接)

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

谢谢你的想法。

最佳答案

据我从您提供的链接中可以看出,您要连接的 Controller 应该由 currentViewController 指向。所以,问题在于这个声明:

@property(weak,nonatomic)UIViewController *currentViewController;

这需要强大,而不是弱。 IBOutlets 弱是可以的,因为指向的对象是另一个 View 的 subview ,该 View 持有对它们的强引用。但是, currentViewController 应该很强大,以防止您的 Controller 被释放(没有其他东西对它有很强的引用)。我从链接中看到他在 perform 方法中将 currentViewController 设置为 segue 的目标 Controller ——如果你这样做,并且你将声明更改为 strong,你应该没问题。

关于iOS 5 自定义标签栏取消分配 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16155030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com