2013-02-22 52 views
0

我打电话从委托类视图控制器的方法,该方法被调用,但UITableView的在方法声明不把它委派方法和一个按钮和观点也ALLOC该观点,但没有显示。呼叫视图控制器的方法,有问题

从begining解释:

在导航控制器所需的按钮,并在所有视图中显示。所以,我以这种方式代表委托人。

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)。

请指导的上方。提前致谢。

+0

所有这些代码在你AppDelegate.m类被称为? – 2013-02-22 10:33:23

+0

否视图控制器类菜单方法 – 2013-02-22 10:37:09

+0

您的UIViewController是否实现了UITableViewDataSource和UITableViewDelegate? – 2013-02-22 10:38:17

回答

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

谢谢,它正在工作。 – 2013-02-22 10:56:15

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

截图Your App Screenshot

+0

谢谢它也在工作。 – 2013-02-22 10:57:09

+0

我想问的一件事是如何隐藏/取消隐藏btnMenuOpen按钮。它可以隐藏的方法在代理中调用,但如何取消隐藏它,我的意思是从哪里。 – 2013-02-22 11:09:58

+0

我无法理解你的问题请解释 – Warewolf 2013-02-22 11:16:52