我在选项卡栏 Controller 中有两个 View Controller (都嵌入在导航 Controller 中)。 ViewControllerA 有两个按钮(MybuttonA 和 MybuttonB,在 Storyboard 中未选中启用框)。 ViewControllerB 是一个 TableViewController。我想在 ViewControllerB 表中选择特定行后启用 ViewControllerA 中的按钮。每个按钮在启用时预计会插入不同的 View Controller 。
我目前的代码仅在 ViewControllersA 和 ViewControllersB 未嵌入导航 Controller 时有效。但是如果没有嵌入导航 Controller ,启用的按钮不会推送 ViewControllers。
ViewControllerA.h
#import <UIKit/UIKit.h>
@interface ViewControllerA : UIViewController {
IBOutlet UIButton * MybuttonA;
IBOutlet UIButton * MybuttonB;
}
-(IBAction)mybuttonaction:(id)sender;
@property(strong,nonatomic)UIButton *MybuttonA;
@property(strong,nonatomic)UIButton *MybuttonB;
@end
ViewControllerA.m
#import "ViewControllerA.h"
@interface ViewControllerA ()
@end
@implementation ViewControllerA
@synthesize MybuttonA;
@synthesize MybuttonB;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(IBAction)mybuttonaction:(id)sender{
//write code to push view controller
}
ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
@interface ViewControllerB : UITableViewController{
ViewControllerA *viewcontrollerA;
}
@property (nonatomic, retain) ViewControllerA *viewcontrollerA;
@end
ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
@interface ViewControllerB () {
}
@end
@implementation ViewControllerB
@synthesize viewcontrollerA;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"CONTENTS";
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(reload) forControlEvents:UIControlEventValueChanged];
[self reload];
[self.refreshControl beginRefreshing];
viewcontrollerA = [self.tabBarController.viewControllers objectAtIndex:0];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
{return 5;}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat: @"Cell #%d", indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString* value = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
if ([value isEqual:@"1"]){
viewcontrollerA.MybuttonA.enabled=YES;
}
else if ([value isEqual:@"2"])
{
viewcontrollerA.MybuttonB.enabled=YES;
}
}
else {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
@end
最佳答案
使用这个:
viewcontrollerA = ((UINavigationController*)[self.tabBarController.viewControllers objectAtIndex:0] ).topViewController
取而代之的是:
viewcontrollerA = [self.tabBarController.viewControllers objectAtIndex:0];
关于ios - 如何在 TableViewController B 中选择行时在 ViewController A 中启用 UI 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947649/