Ios 在objective-c中传递不同对象的最佳方式
我有一个UITableView,在每个标题中,我有一个手势识别器。在手势处理程序中,我想对headerview中的某些内容进行更改Ios 在objective-c中传递不同对象的最佳方式,ios,objective-c,messaging,Ios,Objective C,Messaging,我有一个UITableView,在每个标题中,我有一个手势识别器。在手势处理程序中,我想对headerview中的某些内容进行更改 -(void) sectionHeaderButtonPressed: (UIButton *)sender{ NSLog(@"Header Button pressed"); //UIView *mySubView = [sender.view.subviews objectAtIndex:0]; NSLog(@"Class: %@",[s
-(void) sectionHeaderButtonPressed: (UIButton *)sender{
NSLog(@"Header Button pressed");
//UIView *mySubView = [sender.view.subviews objectAtIndex:0];
NSLog(@"Class: %@",[self class]);
int count = [self.view.subviews count];
NSLog(@"Self.view.subviews: %u",count);
Class buttonClass = NSClassFromString(@"UIButton");
for (int i=0; i < count; i++){
int subViewCount = [[self.view.subviews objectAtIndex:i] subviews].count;
Class className = [[self.view.subviews objectAtIndex:i] class];
NSLog(@"SubView (%u) Class:%@ SubViews: %u",i,className,subViewCount);
for (int j=0; j < subViewCount; j++){
Class className01 = [[[self.view.subviews objectAtIndex:i].subviews objectAtIndex:j ] class];
NSLog(@"----SubSubView (%u) Class:%@",j,[[[self.view.subviews objectAtIndex:i].subviews objectAtIndex:j ] class]);
NSLog(@"Comparing Class: %@ to Class: %@",className01, buttonClass);
if (className01 == buttonClass){
[[[self.view.subviews objectAtIndex:i].subviews objectAtIndex:j ] setTitle:@"ABCDE" forState:UIControlStateNormal];
NSLog(@"found button class");
}
}
}
}
-(无效)节头按钮按下:(UIButton*)发送器{
NSLog(@“按下标题按钮”);
//UIView*mySubView=[sender.view.subviews objectAtIndex:0];
NSLog(@“类:%@,[自身类]);
int count=[self.view.subviews count];
NSLog(@“Self.view.subview:%u”,计数);
Class buttonClass=NSClassFromString(@“UIButton”);
for(int i=0;i
上面的代码是手势处理程序,我可以找到按钮和其他每个子视图,但这真的很麻烦
我似乎对一个对象如何调用另一个对象感到困惑。我想把headerview传给手势处理程序,但不知道怎么做
如何直接发送或访问headerview和/或其子视图。如果我有几个相同的类类型,查看类名不会有多大好处
问题是在另一个对象中调用动作的正确方式。运行所有子视图似乎不是正确的方法。基于类名的id是一种容易出错的方法,因为您可能有许多相同的类
我想有一个“从侧面滑入”选择菜单,并根据用户从菜单中选择的内容修改标题。他们将点击标题,这将调用手势处理程序,这将调用滑出菜单,滑出菜单将调用标题,使其更改其内容
这是最后一个令人困惑的部分,当标题视图内容是完全不同的对象时,如何让菜单更改标题视图内容。最好的方法是使用代理,让您的ViewController为您完成繁重的工作。可以找到一些关于如何创建委托的示例
使用自定义代理协议创建自定义视图 您需要做的第一件事是创建要实现的HeaderView的子类。正如您自己已经说过的,您的代码正变得有点麻烦,这将是创建自定义视图的一个好机会 在这个自定义视图中,根据需要重新创建标题,并使用interface builder或以编程方式连接一些标签/按钮(请记住,如果以编程方式执行此操作,请将UI元素的所有弱指针更改为强,我假设您将使用interface builder) 您的.h应该如下所示:
#import <UIKit/UIKit.h>
@protocol CustomHeaderViewDelegate;
@interface CustomHeaderView : UIView
@property (nonatomic, weak) UILabel *someLabel;
@property (nonatomic, weak) UILabel *someButton;
@property (nonatomic, weak) id<CustomHeaderViewDelegate> delegate;
@end
@protocol CustomHeaderViewDelegate <NSObject>
- (void)customHeaderView:(CustomHeaderView *)customheaderView receivedTouchOnButton:(UIButton *)button;
@end
使viewcontroller符合代理协议 在视图控制器中,您希望从新创建的CustomHeaderView实现委托协议。首先,通过向viewController的.m文件中的接口声明添加
,确保viewController实际上符合新创建的协议。它应该看起来像:
...
#import "CustomHeaderView.h"
@interface YOURVIEWCONTROLLER () <CustomHeaderViewDelegate>
...
将标题添加到表格视图中 在viewForHeaderInSection代码中,执行以下操作:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CustomHeaderView headerView = [[CustomHeaderView alloc] init];
headerView.delegate = self;
/* do some more customisation */
return headerView;
}
查找您的按钮 如果要在节标题中查找按钮或其他视图,只需执行以下操作:
CustomHeaderView *customHeaderView = (CustomHeaderView *)[self tableView:YOURTABLEVIEW viewForHeaderInSection:SOMESECTION];
customHeaderView.button;
最好的方法是使用代理,让您的ViewController为您完成繁重的工作。可以找到一些关于如何创建委托的示例
使用自定义代理协议创建自定义视图 您需要做的第一件事是创建要实现的HeaderView的子类。正如您自己已经说过的,您的代码正变得有点麻烦,这将是创建自定义视图的一个好机会 在这个自定义视图中,根据需要重新创建标题,并使用interface builder或以编程方式连接一些标签/按钮(请记住,如果以编程方式执行此操作,请将UI元素的所有弱指针更改为强,我假设您将使用interface builder) 您的.h应该如下所示:
#import <UIKit/UIKit.h>
@protocol CustomHeaderViewDelegate;
@interface CustomHeaderView : UIView
@property (nonatomic, weak) UILabel *someLabel;
@property (nonatomic, weak) UILabel *someButton;
@property (nonatomic, weak) id<CustomHeaderViewDelegate> delegate;
@end
@protocol CustomHeaderViewDelegate <NSObject>
- (void)customHeaderView:(CustomHeaderView *)customheaderView receivedTouchOnButton:(UIButton *)button;
@end
使viewcontroller符合代理协议 在视图控制器中,您希望从新创建的CustomHeaderView实现委托协议。首先,通过向viewController的.m文件中的接口声明添加
,确保viewController实际上符合新创建的协议。它应该看起来像:
...
#import "CustomHeaderView.h"
@interface YOURVIEWCONTROLLER () <CustomHeaderViewDelegate>
...
将标题添加到表格视图中 在viewForHeaderInSection代码中,执行以下操作:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CustomHeaderView headerView = [[CustomHeaderView alloc] init];
headerView.delegate = self;
/* do some more customisation */
return headerView;
}
查找您的按钮 如果要在节标题中查找按钮或其他视图,只需执行以下操作:
CustomHeaderView *customHeaderView = (CustomHeaderView *)[self tableView:YOURTABLEVIEW viewForHeaderInSection:SOMESECTION];
customHeaderView.button;
为什么不为视图设置一些标记?你得到了标签,你知道它是什么视图。为什么不为视图设置一些标签呢?你得到了标签,你知道它是什么视图。