0

デリゲート クラスからビュー コントローラー メソッドを呼び出しています。メソッドは呼び出されますが、そのメソッドで宣言された uitableview はデリゲート メソッドとは呼び出されず、ボタンとビューもそのビューに割り当てられますが、何も表示されません。

最初から説明:

ナビゲーション コントローラーではボタンが必要で、すべてのビューに表示されます。だから、私はそれをデリゲートでこのように取りました。

UIButton *btnMenuOpen = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuOpen.frame = CGRectMake(0, 15, 40, 56);
[btnMenuOpen setImage:[UIImage imageNamed:@"side_menu.png"] forState:UIControlStateNormal];
[btnMenuOpen addTarget:self action:@selector(menu) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btnMenuOpen];

-(void)menu
{
ViewController *viewC = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
[viewC menu];
}

これは、View Controller クラスです。

-(void)menu
{
NSLog(@"menu opened");

self.mMenuView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240, 480)];
self.mMenuView.backgroundColor = [UIColor grayColor];
self.mMenuView.autoresizingMask = 0;
[self.navigationController.view addSubview:self.mMenuView];

self.mSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 16, 220, 44)];
self.mSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.mSearchBar.backgroundImage = [UIImage imageNamed:@"slide_tab_button.png"];
self.mSearchBar.delegate = self;
[self.mMenuView addSubview:self.mSearchBar];

UIButton *btnMenuClose = [UIButton buttonWithType:UIButtonTypeCustom];
btnMenuClose.frame = CGRectMake(215, 19, 40, 44);
[btnMenuClose setImage:[UIImage imageNamed:@"side_menu.png"] forState:UIControlStateNormal];
[btnMenuClose addTarget:self action:@selector(menu_close) forControlEvents:UIControlEventTouchUpInside];
[self.mMenuView addSubview:btnMenuClose];

self.mMenuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 61,
                                                                   240, 420) style:UITableViewStylePlain];
self.mMenuTableView.delegate = self;
self.mMenuTableView.dataSource = self;
[self.mMenuView addSubview:self.mMenuTableView];

[self.mMenuTableView reloadData];

}

現在、このメソッドが呼び出されたときに何も表示されず、制御はそれを通過しますが、何も起こらず、テーブルのデリゲートは呼び出されず、他のものも動作しません (ボタンの検索バーと uiview)。

以上をご案内いたします。前もって感謝します。

4

2 に答える 2

1

NSNotificationCenterこのように使ってみてください

ViewController.mに通知を登録します

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(menu)
                                                 name: @"myMenuNotification"
                                               object: nil];
}

そして、AppDelegate.mで、次のような通知を使用してそのメソッドを呼び出します。

-(void)menu
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"myMenuNotification" object:nil];
}
于 2013-02-22T10:39:12.977 に答える
1

どのように使っているかわかりません。しかし、いくつかの変更を加えてコードをチェックしました。それは正常に動作しています。変更されたコードは次のとおりです。

AppDelegate メソッド内。

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

    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];

    UIButton *btnMenuOpen = [UIButton buttonWithType:UIButtonTypeCustom];
    btnMenuOpen.frame = CGRectMake(0, 15, 40, 56);
    [btnMenuOpen setImage:[UIImage imageNamed:@"5.jpg"] forState:UIControlStateNormal];
    [btnMenuOpen addTarget:self action:@selector(menu) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btnMenuOpen];

    [self menu];
    return YES;
}

-(void)menu
{
    [self.viewController menu];
}

//ビューコントローラーで............

    -(void)menu
{
    NSLog(@"menu opened");

    UIView *mMenuView;
    UISearchBar *mSearchBar;
    UITableView *mMenuTableView;

    mMenuView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 240, 480)];
    mMenuView.backgroundColor = [UIColor grayColor];
    mMenuView.autoresizingMask = 0;
    [self.navigationController.view addSubview:mMenuView];

    mSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 16, 220, 44)];
    mSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    mSearchBar.backgroundImage = [UIImage imageNamed:@"5.jpg"];
//    mSearchBar.delegate = self;
    [mMenuView addSubview:mSearchBar];

    UIButton *btnMenuClose = [UIButton buttonWithType:UIButtonTypeCustom];
    btnMenuClose.frame = CGRectMake(215, 19, 40, 44);
    [btnMenuClose setImage:[UIImage imageNamed:@"side_menu.png"] forState:UIControlStateNormal];
    [btnMenuClose addTarget:self action:@selector(menu_close) forControlEvents:UIControlEventTouchUpInside];
    [mMenuView addSubview:btnMenuClose];

    mMenuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 61, 240, 420) style:UITableViewStylePlain];
//    mMenuTableView.delegate = self;
//    mMenuTableView.dataSource = self;
    [mMenuTableView setBackgroundColor:[UIColor blueColor]];
    [mMenuView addSubview:mMenuTableView];

    [mMenuTableView reloadData];
}

スクリーンショットアプリのスクリーンショット

于 2013-02-22T10:42:55.357 に答える