NSViewController.view(Subviews)setTag

时间:2012-06-12 14:04:00

标签: objective-c cocoa nsview subview nsviewcontroller

每次按下按钮,mainController都会调用[self.view addSubview:createCustomView.view]。这里一切都很好。问题是我需要在我创建的每个子视图上放置一个标记,以便以后检索它们。我已经尝试过了:

MainController.m

NSNumber *i;
createCustomView.view.tag = i; //readonly

我真正想做的是:

int i;
[createCustomView.view setTag:i];

但是setTag方法不存在。我的问题是:除了使用标识符字符串之外我有什么方法可以做到这一点,这会给我的案例带来一些问题吗?

提前致谢

这是Controller的.h文件

#import <Foundation/Foundation.h>
#import "TransactionButtonView.h"
@class TransactionButtonController;
@interface TransactionViewController : NSViewController
{
TransactionButtonController *transactionButtonController;
}
-(IBAction)createOnPushButton:(id)sender;
-(void)recalculatePositionOnRemove:(long)tag;

@property (nonatomic,assign) TransactionButtonController *transactionButtonController;

@end

这是Controller的.m文件

#import "TransactionViewController.h"
#import "TransactionButtonController.h"
#import "MainController.h"
@implementation TransactionViewController
@synthesize transactionButtonController;

-(IBAction)createOnPushButton:(id)sender
{
transactionButtonController = [[TransactionButtonController alloc] initWithNibName:@"TransactionButton" bundle:nil];
NSPoint originPoint;

for (int i=1; i <= [[self.view subviews]count]; i++) {
        originPoint.y = transactionButtonController.view.bounds.origin.y + self.view.bounds.size.height - transactionButtonController.view.bounds.size.height*i;
transactionButtonController.view.tag = i; // Here's the PROBLEM!!!
    [[transactionButtonController view]setIdentifier:[[NSNumber numberWithInt:i]stringValue]]; //here's the not good option

}
originPoint.x = transactionButtonController.view.bounds.origin.x;
[[transactionButtonController view] setFrameOrigin:originPoint];
[self.view addSubview:transactionButtonController.view];
[transactionButtonController sendVarsToButton:@"xxx" :@"591" :5 :87456356472456];

}

-(void)recalculatePositionOnRemove:(long)tag
{
NSPoint originPoint;
for (long i = tag; i<[[self.view subviews]count]; i++) {

    originPoint.y = transactionButtonController.view.bounds.origin.y +self.view.bounds.size.height - transactionButtonController.view.bounds.size.height*i;
    originPoint.x = transactionButtonController.view.bounds.origin.x;


     [[transactionButtonController.view viewWithTag:i+1] setFrameOrigin:originPoint];
}
}

@end 

1 个答案:

答案 0 :(得分:0)

如果要为视图添加标记,请执行以下操作:

theView.tag = 1;

删除它:

[[myParentView viewWithTag:1] removeFromSuperview]