1

私は iOS 開発が初めてで、辞書を作成しようとしています。私の最初の画面では、上部に検索バーがあり、TableViewその下にすべてのエントリがあります。検索バーに入力すると、一致するエントリで TableView がフィルタリングされます。リストすべてモードまたはフィルター モードでいずれかのセルをクリックすると、別のビューがプッシュされ、単語の名前と定義が表示されます。

表のセルをクリックするたびに新しい画面を開くことができますが、何らかの理由で、新しい画面の 2 つのラベル (単語と定義) が選択内容で更新されず、空のままになります。 . IBOutletただし、これら 2 つの を XIB ファイルに正しく設定したと思います。なぜなら、 のtext属性をログに記録するとUILabel、それらが正しく出力されるからです。ヘルプ?

EntryViewController.h (2 番目の画面):

#import <UIKit/UIKit.h>

@interface EntryViewController : UIViewController

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

@end

EntryViewController.m:

#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.h (最初の画面):

#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

UTVTViewController.m

#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

編集: AppDelegate のヘッダーと実装ファイルを含めています。エラーは、現在何にも接続していないのどこかにあると感じていますがnavigationController、XIB にエラーがないため、どこに接続すればよいかさえわかりません。

UTVTAppDelegate.h:

@interface UTVTAppDelegate : UIResponder <UIApplicationDelegate>

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

@end

UTVTAppDelegate.m

@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];
    self.window.rootViewController = self.viewController;

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

    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

// default Xcode methods

@end
4

4 に答える 4

0

サブビューとして追加するのではなく、ナビゲーションコントローラーをウィンドウのルートビューコントローラーとして設定してみてください。

@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;
}
于 2013-01-16T10:32:26.377 に答える
0

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

UI から切り離された新しいオブジェクトを作成します。NIB が読み込まれると、すべての UI オブジェクトが作成され、IBOutlet プロパティがそれらで初期化されます。この 2 行を削除するだけで問題ありません。

于 2013-01-16T08:40:07.113 に答える
0

didSelectRowAtIndexPathメソッドでは、View Controller を Navigation Controller にプッシュする前に のテキストを設定していましUILabelた。解決:

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

allocまた、デタッチされたUILabelオブジェクトを呼び出したコードの行も削除しました。

于 2013-01-17T05:20:38.213 に答える
0

UILabels を xib で作成した場合は、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;

}

于 2013-01-16T08:38:20.280 に答える