2012-06-04 21 views
0

我建立它使用一個程序生成UITableView彈出並填充UITextField的應用程序。當用戶觸摸UITextField有效值的TableView中彈出。 UITableView有部分和一個搜索欄。我從另一個我寫的非常類似的應用程序的代碼,它工作正常。UITableView的子類不執行初始化過去

UITextField被觸摸時,執行在UITableViewCatagoryTableViewController.m)開始,但只有init方法被調用,則控制返回到調用類(ViewController.m)。我已經重寫了UITextField的關鍵彈出窗口。

沒有爲ViewController一個XIB而不是CatagoryTableViewController。的UITextField觸地映射到catagoryFieldPressed方法。

順便提一下,SFCLog定義爲:

define SFCLog(FORMAT, ...) fprintf(stderr,"%s [line %d] %s\n",__PRETTY_FUNCTION__, __LINE__,[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]) 

我在CatagoryTableViewController放置SFCLog日誌(的NSLog的變型)在每一個方法的開始,並且僅init的以往調用。關於我失蹤的任何想法?

ViewController.h(呼叫類的頭)

@interface ViewController : UIViewController <UITextFieldDelegate> 
{ 
    Word *currentWord; 
    NSMutableArray *words; 
    int wordsIndex; 
    int viewState; 
    int correctCounter; 
    int incorrectCounter; 
} 

ViewController.m(呼叫類)

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    SFCLog(@""); 
    return NO; // Hide both keyboard and blinking cursor. 
} 

- (IBAction)catagoryFieldPressed:(id)sender 
{ 
    SFCLog(@"Catagory button"); 
    CatagoryTableViewController *childView = [[CatagoryTableViewController alloc] init]; 
    childView.parentView = self; 
    [self.navigationController pushViewController:childView animated:YES]; 
    SFCLog(@"Returned from Catagory button"); 
} 

CatagoryTableViewController.h

@class ViewController; 

@interface CatagoryTableViewController : UITableViewController <UISearchBarDelegate> 
{ 
    NSMutableDictionary *catagoryDictionary; 
    NSArray *filteredArray; 
    UISearchBar *searchBar; 
    UISearchDisplayController *searchController; 
    NSMutableArray *sectionArray; 
} 

@property (nonatomic, weak) ViewController *parentView; 

CatagoryTableViewController.m更多的代碼比這個,但這些方法應該解僱。

@implementation CatagoryTableViewController 
@synthesize parentView; 


- (id) init 
{ 
    SFCLog(@""); 
    if ((self = [super init])) 
    { 
     SFCLog(@"in here");   
    } 
    SFCLog(@""); 
    return self; 
} 


- (void)viewDidLoad 
{ 
    SFCLog(@""); 
    [super viewDidLoad]; 
} 

-(void)loadView 
{ 
    SFCLog(@"load view"); 
    [super loadView]; 
    self.navigationItem.hidesBackButton = TRUE; 

    // Load the catagory dictionary from the database 
    WordService *wordService = [[WordService alloc] init]; 
    NSMutableArray *rawCatagories = [wordService getCatagories]; 
    catagoryDictionary = [NSMutableDictionary dictionary]; 
    for (WordCatagory *wordCatagory in rawCatagories) { 
     [catagoryDictionary setObject:wordCatagory forKey:wordCatagory.wordCatagory]; 
    } 

    // Section Array 
    sectionArray = [NSMutableArray array]; 
    for (int i = 0; i < 26; i++) 
     [sectionArray addObject:[self itemsInSection:i]]; 
    SFCLog(@"sectionArray: %@",sectionArray); 


    // Create a search bar 
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)]; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
    searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    searchBar.keyboardType = UIKeyboardTypeAlphabet; 
    self.tableView.tableHeaderView = searchBar; 

    // Create the search display controller 
    searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
    searchController.searchResultsDataSource = self; 
    searchController.searchResultsDelegate = self; 
} 

調試控制檯輸出

[ViewController catagoryFieldPressed:] [line 223] Catagory button 
[CatagoryTableViewController init] [line 18] 
[CatagoryTableViewController init] [line 21] in here 
[CatagoryTableViewController init] [line 23] 
[ViewController catagoryFieldPressed:] [line 227] Returned from Catagory button 
[ViewController textFieldShouldBeginEditing:] [line 33] 

每菲利普·米爾斯的職務加入的NSLog編輯進行調試控制檯輸出。

-[ViewController catagoryFieldPressed:] [line 223] Catagory button 
-[CatagoryTableViewController init] [line 18] 
-[CatagoryTableViewController init] [line 21] in here 
-[CatagoryTableViewController init] [line 23] 
-[CatagoryTableViewController loadView] [line 171] load view 
2012-06-04 18:52:57.930 Fluent[1835:f803] Opened database 
2012-06-04 18:52:57.930 Fluent[1835:f803] Path: /Users/sean/Library/Application Support/iPhone Simulator/5.1/Applications/8B93174F-8836-4533-BCFF-F98AC2F34F26/Documents/fluent_tr.sqlite 
-[WordService getCatagories] [line 114] 
-[CatagoryTableViewController itemsInSection:] [line 46] 
(edited repeating NSLogs to shorten) 
-[CatagoryTableViewController firstLetter:] [line 39] 
-[CatagoryTableViewController numberOfSectionsInTableView:] [line 99] 
-[CatagoryTableViewController tableView:titleForHeaderInSection:] [line 86] 
-[CatagoryTableViewController firstLetter:] [line 39] 
-[CatagoryTableViewController tableView:numberOfRowsInSection:] [line 108] 
2012-06-04 18:52:57.937 Fluent[1835:f803] Rows per section count: 1 
-[CatagoryTableViewController viewDidLoad] [line 31] 
2012-06-04 18:52:57.938 Fluent[1835:f803] Controller: <CatagoryTableViewController: 0x884c880>, View: <UITableView: 0xbb90a00; frame = (0 20; 320 460); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x884ce20>; contentOffset: {0, 0}> 
-[ViewController catagoryFieldPressed:] [line 228] Returned from Catagory button 
-[ViewController textFieldShouldBeginEditing:] [line 33] 

AppDelegate.m之前

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    UIViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    UIViewController *numberTestViewController = [[ViewController alloc] initWithNibName:@"NumberTestViewController" bundle:nil]; 

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController]; 
    nav.tabBarItem.title = @"Words"; 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController,numberTestViewController, nil]; 
    self.tabBarController.delegate = self; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    NumberTestViewController *numberTestViewController = [[NumberTestViewController alloc] initWithNibName:@"NumberTestViewController" bundle:nil]; 

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController]; 
    self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav,numberTestViewController, nil]; 
    self.tabBarController.delegate = self; 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

當您按下視圖控制器時,**視覺是否會發生**任何事情?如果您在推送之前插入類似於NSLog(@「Controller:%@,View:%@」,childView,childView.view);''的其他方法,您會看到其他方法嗎? –

+0

按下視圖控制器時不會發生視覺事件。你的NSLog非常有用:當我添加它時,它顯示了通過所有相關方法的控制流程。我認爲我的部分數組有問題,因爲零部分正在返回。任何想法爲什麼在這些情況下Log語句不起作用? – ardochhigh

+0

插入NSLog後添加的其他日誌。 – ardochhigh

回答

0

後發現的問題,與菲利普·米爾斯的幫助。調用UITableView的視圖控制器未正確添加到導航堆棧。它已被實例化,然後添加到navigationController通過UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];但導航控制器本身沒有正確添加到tabBarController。

請參閱上述AppDelegate.m的前後版本。

感謝您的幫助菲利普。

+0

很高興你找到它。顯式引用控制器視圖來強制加載並隔離哪部分進程出錯的調試技巧已經有用了很多次。 –