0

我正在构建一个应用程序,它使用以编程方式生成UITableView的弹出和填充UITextField. 当用户触摸UITextField一个有效值的 TableView 时应该会弹出。有UITableView部分和一个搜索栏。我从另一个我编写的非常相似的应用程序中获取代码,它运行良好。

UITextField触摸到时,在UITableViewCatagoryTableViewController.m)中开始执行,但只调用了init方法,然后控制权返回到调用类(ViewController.m)。我已经覆盖了 UITextField 的键弹出窗口。

有一个 XIB 用于ViewController但不用于CatagoryTableViewController. UITextField Touch Down 映射到catagoryFieldPressed方法。

顺便说一句,SFCLog 定义为:

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

我已将 SFCLog 日志(NSLog 的一种变体)放在 中的每个方法的开头CatagoryTableViewController,并且只调用了 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] 

根据 Phillip Mills 的帖子添加 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;
}
4

1 回答 1

0

在 Phillip Mills 的帮助下发现了问题。调用 UITableView 的视图控制器未正确添加到导航堆栈。它已被实例化,然后通过添加到 navigationControllerUINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];但导航控制器本身未正确添加到 tabBarController。

请参阅上面的 AppDelegate.m 的前后版本。

感谢您的帮助菲利普。

于 2012-06-04T20:02:43.677 回答