2

Scott Sherwood のこのチュートリアル ( http://www.scott-sherwood.com/ios-5-creating-a-custom-side-tabbar-using-storyboards-and-custom-segues/ ) に従って、サブビューを使用して他のビューにロードするカスタム タブ バー。すべてが美しく機能し、結果に非常に満足しています。ただし、「タブ ページ」の 1 つにテーブル ビューを追加してデータ ソースに接続すると、アプリがクラッシュします。

ARC を使用しており、ゾンビをオンにしているため、テーブル ビューの割り当てが解除されたインスタンスにメッセージが送信されたことがコンソールに表示されます。テーブルビューを保持する方法がわからないので、データが表示されます。これは私の 2 番目のアプリであり、ストーリーボードと高度なビュー コントローラーにはまだ慣れていません。私は何が欠けていますか?

これが私のカスタムタブビューコントローラーです:

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

そして、テーブル ビューを含むタブに読み込まれるページ: (注*: このページは、ストーリーボードでテーブル ビュー データ ソースおよびテーブル ビュー デリゲートとしてリンクされています)

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

ご意見ありがとうございます。

4

3 に答える 3

0

あなたが抱えている問題は、特に currentViewController の場合、強いプロパティではなく弱いプロパティを持っていることが原因であると推測しています。それらをすべて強力に変更してみて、まだ問題があるかどうかを確認してください。

出典: Objective-C の弱いプロパティ セッター属性と強いプロパティ セッター属性

弱い (iOS4 = unsafe_unretained )

  • 「他の誰かがそれを強く指摘している限り、これを保持してください」と表示されますが、割り当てと同じであり、保持または解放はありません
  • 「弱い」参照とは、保持しない参照です。
  • 通常、IBOutlets (UIViewController の子) には weak を使用します。これは、親オブジェクトが存在する限り、子オブジェクトが存在するだけでよいため機能します。
  • 弱い参照とは、参照先のオブジェクトをガベージ コレクターによるコレクションから保護しない参照です。
  • Weak は本質的に割り当て、保持されないプロパティです。オブジェクトの割り当てが解除された場合を除いて、ウィーク ポインターは自動的に nil に設定されます。
于 2013-04-22T21:39:32.617 に答える
0
@property (nonatomic, strong) IBOutlet UITableView *tableView;

このプロパティをストーリーボードのテーブルビューに接続します。ビューコントローラーには強いポインターがあるため、これにより、テーブルビューの解放が停止するはずです。デバッグ ポイントから、エラーの原因と思われる領域にブレーク ポイントを配置して、コードが壊れている行を特定します。その後、エラーをより深く調査できます。これが役立つことを願っています。

于 2013-04-22T23:26:48.093 に答える