-6

メインコントローラーからデータセルコントローラーファイルにcountの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];

// SubViewクラスであるdataColumnを呼び出しており、その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

appDelegateを使用して呼び出す場合は、MysetValueに移動します AppDelegate

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

いくつかの考え:

  1. あなたはMysetValue少し冗長なメソッドを書いています。プロパティを合成するcolumNumberと、これを行うセッターメソッドが作成setColumNumberされます。必要がない場合は、独自のセッターを作成しないでください。

  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が役立つプロパティの値の変更に関するクラス間のコミュニケーションに焦点を当てていると思いました。しかし、これはより高度なトピックであり、現時点ではそれについて心配する必要はありません。それは、新しい開発者にとって混乱を招くだけです。

    余談ですが、自動生成されたセッターメソッドを使用する利点の1つは、キー値監視(KVO)に自動的に準拠することです。(独自のセッターを作成する場合は、KVOをサポートするために必要なコーディングを行うこともできますが、ここで行う理由はありません。標準のセッターを使用してください。)キー値監視プログラミングガイドを参照してください。結論として、クラスは、別のクラスのプロパティの変更のオブザーバーとして自分自身を登録できます。


プロパティの使用方法の例を確認したい場合は、次のビューサブクラスを検討してください。

DetailView.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

DetailView.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

次に、これらのビューを追加するビューコントローラを作成できますが、1つをタップすると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 に答える