-6

我想将计数的 int 值从我的主控制器设置为数据单元控制器文件。我现在不想调用那个数据单元控制器文件我想稍后调用那个数据单元 M 控制器文件,但是我想从我的主控制器文件中设置计数整数,这样当我调用我的数据单元文件时它需要计数整数我从我的主控制器设置并使用它。

我的 dataViewController.m

- (void)viewDidLoad {
[super viewDidLoad];

UIView *backgroundCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
backgroundCoverView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
toggleSwitch1 = [UIButton buttonWithType:UIButtonTypeCustom];
toggleSwitch2 = [UIButton buttonWithType:UIButtonTypeCustom];

[self.view addSubview:backgroundCoverView];

// Add the 6 buttons and columns.
NSMutableArray *columnsMutable = [NSMutableArray arrayWithCapacity:6];
NSMutableArray *buttonsMutable = [NSMutableArray arrayWithCapacity:6];
for (int i=0; i<6; i++) {



    DataCell* app = (DataCell*)[[UIApplication sharedApplication] delegate];
    [app MysetValue:6];

    DataColumn *column = [[DataColumn alloc] initWithFrame:CGRectMake(86+i*124+(i>=3?100:0), 230, 110, 382)];
    column.cellsVisible = NO;
    if (i>0) {
        column.hidden = YES;
    }
    [self.view addSubview:column];
    [columnsMutable addObject:column];

//我正在调用dataColumn,它是SubView类,从那个dataColumn子视图类我调用数据单元类将单元格填充到列中

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {

    //self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mixBackgroundTile.png"]];
    // this commented line is used to display dotted background in percentage slider bar 

    // Add cells.
    NSMutableArray *mutableCells = [NSMutableArray arrayWithCapacity:5];
    for (int i=4; i>=0; i--) {
        DataCell *cell = [[DataCell alloc] initWithFrame:CGRectMake(100, i*(74+3), 109, 74)]; // data order fix
         //VSLDataCell *cell = [[VSLDataCell alloc] initWithFrame:CGRectMake(-90, i*(74+3), 109, 74)]; for left hand side cell

        [mutableCells addObject:cell];
        [self addSubview:cell];
    }
    cells = [NSArray arrayWithArray:mutableCells];  
}
return self;
 }

现在在数据单元类中,我想根据单元格号和列号分配不同的图像,所以我想知道我现在是哪一列

4

2 回答 2

0

几个想法:

  1. 您编写了一个方法,MysetValue这有点多余。当您合成您的columNumber属性时,它会为您编写一个 setter 方法,setColumNumber它会为您执行此操作。如果不需要,请不要编写自己的 setter。

  2. 如果你有一个属性,columNumber你已经在你的 .h 中定义,并在你的 .m 中合成AppDelegate(或者任何你称之为应用委托类的东西),你现在可以通过以下方式设置这个属性:

    AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [app setColumNumber:6];
    

    或等效地,

    AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    app.columNumber = 6;
    
  3. DataCell但是,根据您明确的答案,您的课程不是应用程序委托课程。显然这是一些UIView基础类。您可以为此类定义自定义属性DataCell,但不能通过[UIApplication sharedApplication]. 您显然将这些DataCell对象保存在一个名为cells. 因此,如果您想设置第一个单元格的columNumber属性,您可以执行以下操作:

    DataCell *cell = cells[0];
    [cell setColumNumber:6];
    
  4. 当我第一次阅读这个问题时,我以为您关注的是类之间关于属性更改值的通信,KVO 对此很有用。但这是一个更高级的话题,我们现在不应该担心这一点。这只会导致新开发人员的困惑。

    顺便说一句,使用自动生成的 setter 方法的优点之一是它自动符合键值观察 (KVO)。(如果您编写自己的 setter,也可以进行必要的编码以支持 KVO,但这里没有理由这样做;使用标准 setter。)请参阅Key-Value Observing Programming Guide。归根结底,一个类可以将自己注册为另一个类的属性变化的观察者。


如果您想查看如何使用属性的示例,请考虑以下视图子类:

细节视图.h:

#import <UIKit/UIKit.h>

// public interfaces belong here

@interface DetailView : UIView

@property (nonatomic, readonly) NSInteger columnNumber; // column number set during initialization
@property (nonatomic)           NSInteger tapCount;     // a count of how many times we tapped on this

- (id)initWithColumnNumber:(NSInteger)column;           // interface for creating DetailView

@end

详细视图.m:

#import "DetailView.h"

@implementation DetailView

const CGFloat kWidth = 40.0f;
const CGFloat kMargin = 5.0f;
const CGFloat kHeight = 300.0f;

// create view, set column number, set frame according to column number

- (id)initWithColumnNumber:(NSInteger)column
{
    CGRect frame = CGRectMake(kMargin + column * (kWidth + kMargin), kMargin, kWidth, kHeight);
    self = [super initWithFrame:frame];
    if (self) {
        _columnNumber = column;
        _tapCount = 0;
        self.backgroundColor = [UIColor grayColor];
    }
    return self;
}

@end

然后我可以编写一个视图控制器来添加这些视图,但是当你点击一个时,它会NSLog为我们提供列号并告诉我们我们点击了多少次:

#import "ViewController.h"
#import "DetailView.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (NSInteger column = 0; column < 5; column++)
    {
        DetailView *detailView = [[DetailView alloc] initWithColumnNumber:column];
        [self.view addSubview:detailView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(handleTap:)];
        [detailView addGestureRecognizer:tap];
    }
}

- (void)handleTap:(UITapGestureRecognizer *)gesture
{
    DetailView *detailView = (id)gesture.view;
    NSAssert([detailView isKindOfClass:[DetailView class]], @"%s: Not a DetailView: %@", __FUNCTION__, detailView);

    detailView.tapCount++;

    NSLog(@"%s: column number = %d; tapped %d times", __FUNCTION__, detailView.columnNumber, detailView.tapCount);
}

@end
于 2013-03-27T13:14:22.783 回答
0

MysetValue如果您想AppDelegate使用 appDelegate 调用它,请移至

AppDelegate* appdelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appdelegate MysetValue:6]; 
于 2013-03-27T13:08:54.653 回答