2013-02-22 57 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