单击TableView中的单元格时UILabel不会更新

时间:2013-01-16 08:16:16

标签: iphone ios objective-c

我是iOS开发的新手,我正在尝试构建一个字典。我的第一个屏幕顶部有一个搜索栏,下面是所有条目的TableView。当您在搜索栏中键入时,将使用匹配的条目筛选TableView。当您在列表全部模式或过滤模式下单击任何单元格时,将按下另一个视图,显示单词的名称和定义。

每当我点击表格单元格时,我就能够打开一个新的屏幕,但由于某种原因,新屏幕中的两个标签(单词和定义)将不会随选择更新 - 它保持空白。我想我已经在我的XIB文件中正确设置了这两个IBOutlet,因为当我记录text的{​​{1}}属性时,它们会正确打印出来。帮助

EntryViewController.h(第二个屏幕):

UILabel

EntryViewController.m:

#import <UIKit/UIKit.h>

@interface EntryViewController : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *word;
@property (nonatomic, strong) IBOutlet UILabel *definition;

@end

UTVTViewController.h(第一个屏幕):

#import "EntryViewController.h"

@implementation EntryViewController

@synthesize word, definition;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    self.word = [[UILabel alloc] init];
    self.definition = [[UILabel alloc] init];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

UTVTViewController.m

#import <UIKit/UIKit.h>
#import "EntryViewController.h"

@interface UTVTViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,
    UISearchBarDelegate, UISearchDisplayDelegate>

@property (nonatomic, strong) IBOutlet UITableView *sampleTableView;
@property (nonatomic, strong) NSMutableDictionary *entries;
@property (nonatomic, strong) NSMutableArray *sortedWords, *filteredList;
@property (nonatomic) BOOL isSearching;
@property (nonatomic, retain) EntryViewController *entryView;

- (void)filterListForSearchText:(NSString *)searchText;

@end

编辑:我包含了AppDelegate的标头和实现文件。我有一种感觉错误在#import "UTVTViewController.h" #import "EntryViewController.h" @implementation UTVTViewController @synthesize sampleTableView, entries, sortedWords, filteredList, isSearching, entryView; - (void)viewDidLoad { [super viewDidLoad]; entries = [NSMutableDictionary new]; [entries setObject:@"a syndrome of wide spaced eyes (ocular hypertelorism), front-facing (anteverted) nostrils, a broad upper lip, a malformed (\"saddle-bag\") scrotum, and laxity of the ligaments resulting in bending back of the knees (genu recurvatum), flat feet, and overly extensible fingers. There are X-linked and autosomal forms of the disease. The gene for the X-linked form has been mapped to chromosome band Xp11.21 and identified as the FGD1 gene." forKey:@"aarskog-scott syndrome"]; [entries setObject:@"a diminution, decrease or easing. In medicine there may be abatement of pain or any other symptom or sign. In the environment there may abatement in the degree of pollution" forKey:@"abatement"]; [entries setObject:@"a disorder marked by a pathological pattern of alcohol use that causes serious impairment in social or occupational functioning. It includes both alcohol abuse and alcohol dependence." forKey:@"alcoholism"]; // ...other words sortedWords = [[NSMutableArray alloc] initWithArray:[[entries allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]]; filteredList = [[NSMutableArray alloc] init]; isSearching = NO; } // ... #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *selection; if (isSearching && [filteredList count]) { selection = [filteredList objectAtIndex:indexPath.row]; } else { selection = [sortedWords objectAtIndex:indexPath.row]; } EntryViewController *evc = [[EntryViewController alloc] initWithNibName:@"EntryView" bundle:nil]; evc.title = selection; [evc.word setText:selection]; [evc.definition setText:[entries objectForKey:selection]]; NSLog(@"word=%@", evc.word.text); NSLog(@"definition=%@", evc.definition.text); [[self navigationController] pushViewController:evc animated:YES]; } @end 的某个地方,我目前没有连接任何东西,但我甚至不知道在哪里附上它,因为我的XIB中没有。

UTVTAppDelegate.h:

navigationController

UTVTAppDelegate.m

@interface UTVTAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UTVTViewController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

4 个答案:

答案 0 :(得分:0)

如果您通过xib创建了UILabel,则不应在init中重新分配它。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self.word = [[UILabel alloc] init];//REMOVE
self.definition = [[UILabel alloc] init];//REMOVE
return self;

}

答案 1 :(得分:0)

self.word = [[UILabel alloc] init];
self.definition = [[UILabel alloc] init];

您创建与UI分离的新对象。加载NIB后,将为您创建所有UI对象,并使用它们初始化您的IBOutlet属性。只需删除这两行就可以了。

答案 2 :(得分:0)

尝试将navigationcontroller设置为窗口的rootviewController,而不是将其添加为子视图。

@implementation UTVTAppDelegate

@synthesize navigationController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[UTVTViewController alloc] initWithNibName:@"UTVTViewController" bundle:nil];

    navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    [navigationController setNavigationBarHidden:YES];

    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

答案 3 :(得分:0)

didSelectRowAtIndexPath方法中,我在将视图控制器推送到导航控制器之前设置了UILabel的文本。解决方案:

[[self navigationController] pushViewController:evc animated:YES];
[evc.word setText:selection];
[evc.definition setText:[entries objectForKey:selection]];

我还删除了代码中的行,我在分离的alloc对象上调用了UILabel