【问题标题】:How to enable a UI botton in ViewController A upon on row selection in TableViewController B. Both View controllers are nested in TabarController如何在表视图控制器 B 中的行选择时启用视图控制器 A 中的 UI 按钮。两个视图控制器都嵌套在 TabbarController 中
【发布时间】:2013-05-30 05:28:14
【问题描述】:

我在标签栏控制器中有两个视图控制器。 ViewControllerA 有两个按钮(MybuttonA 和 MybuttonB,情节提要中未选中启用框)。 ViewControllerB 是一个 TableViewController。我想在 ViewControllerB 表中选择特定行后启用 ViewControllerA 中的按钮。

非常感谢任何帮助...

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{
NSString * link = @"https://google.com";

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]];
 }

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

}

#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

【问题讨论】:

    标签: ios uitableview uibutton uitabbarcontroller


    【解决方案1】:

    ViewControllerB 中,将以下行添加到viewDidLoad

    viewControllerA = [self.tabBarController.viewControllers objectAtIndex:0];
    

    然后,您的其余代码应该可以正常工作。

    请注意,除非您有充分的理由,否则您通常应该使用 self.viewControllerA 而不是直接访问 iVar。 :-)

    【讨论】:

    • 我已经实现了这两个建议并且代码在断点处停止
    • self.viewcontrollerA.MyButtonA.enabled=YES;
    • 如果代码在断点处停止,则点击运行使其继续。 :)
    • 现在我又遇到了麻烦。我想将 ViewControllerA 和 ViewControllerB 都嵌入到 NavigationControllers 中,而 NavigationControllers 又嵌套在 tabbarcontroller 中。
    • 我尝试了几种代码变体 - 但按钮未启用。如果我删除导航控制器,Mybutton 不会推送另一个 ViewControllerC!我不知道该怎么做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 2023-03-29
    • 2013-07-04
    • 2014-05-25
    相关资源
    最近更新 更多