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];
}

这是视图控制器类:

-(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 回答