【问题标题】:How to Enable UI botton in ViewController A upon on row selection in TableViewController B如何在 TableViewController B 中的行选择时启用 ViewController A 中的 UI 按钮
【发布时间】:2013-06-01 14:36:58
【问题描述】:

我在标签栏控制器中有两个视图控制器(都嵌入在导航控制器中)。 ViewControllerA 有两个按钮(MybuttonA 和 MybuttonB,情节提要中未选中启用框)。 ViewControllerB 是一个 TableViewController。我想在 ViewControllerB 表中选择特定行后启用 ViewControllerA 中的按钮。每个按钮在启用时都应该推送不同的视图控制器。

我目前的代码仅在 ViewControllersA 和 ViewControllersB 未嵌入导航控制器时才有效。但是如果没有导航控制器嵌入,启用的按钮不会推动 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

【问题讨论】:

    标签: ios uinavigationcontroller uibutton uitableview uitabbar


    【解决方案1】:

    使用这个:

    viewcontrollerA = ((UINavigationController*)[self.tabBarController.viewControllers objectAtIndex:0] ).topViewController
    

    而不是这个:

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

    【讨论】:

    • 效果很好。但我有一个语义错误:不兼容的指针类型从 'UIViewController*' 分配给 'ViewControllerA*_strong'
    • viewcontrollerA = ((ViewControllerB*)((UINavigationController*)[self.tabBarController.viewControllers objectAtIndex:0] ).topViewController)
    • 感谢您的提示。以下工作 - viewcontrollerA = ((ViewControllerA*)((UINavigationController*)[self.tabBarController.viewControll‌​ers objectAtIndex:0] ).topViewController)
    【解决方案2】:

    使用委托或“NSNotification”在对象之间传递消息。为类 B 创建一个协议,并在选择其中的特定行时向其委托发送消息,并使视图控制器 A 符合此协议。当视图控制器 A 收到来自视图控制器 B 的委托消息时,在视图控制器 A 中进行更改。

    或者对于松散的广播,您可以在视图控制器 B 中触发/发布关于行选择的通知,并使视图控制器 A 成为它的侦听器,并在发布通知时更改视图。

    尽管在您的情况下委托是要走的路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多