我从委托类调用视图控制器方法,该方法被调用但是在该方法中声明的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)。
请指导以上内容。提前谢谢。
答案 0 :(得分: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];
}
答案 1 :(得分: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];
}
屏幕截图