0

Each time I press a button, mainController is calling [self.view addSubview: createCustomView.view]. Everything works fine here. The problem is that I need to put a tag on each subview I create in order to retrieve them later. I've already tried this :

MainController.m

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

And what I actually wanna do is :

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

But the setTag method doesn't exist. My question is : Is there a way I can do this other than using identifier string which brings some problems in my case?

Thanks in advance

Here's the .h file of the Controller

#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

Here's the .m file of the Controller

#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 
4

1 回答 1

0

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

theView.tag = 1;

要删除它:

[[myParentView viewWithTag:1] removeFromSuperview]
于 2012-06-12T14:07:41.490 回答