2014-02-18 8 views
1

메소드가 호출 될 때마다 객체의 새 인스턴스를 만들어야합니다. 그래서 newInstance1과 같은 변수 카운터의 존재를 확인하고 그것이 발견되면 newInstance2를 생성하는 루프를 생성해야합니다. 어떻게 nsstring에서 클래스를 만들 수 있습니까? 목표는 객체를 표현한 격자에서 드래그 할 수있는 뷰를 만드는 것입니다. 여기에 새 개체 볼NSString을 사용하여 클래스 인스턴스 만들기

//method creates view with new fixtureIcon 
- (IBAction)makeView:(id)sender { 


    rectString = [NSString stringWithFormat:@"0,120,%f,%f", pm.fixWidth, pm.fixHeight]; 
    _try = [[fixtureIcon alloc]init]; 

    _try.fixImg = pm.fixImage; 
    _try.name = pm.name; 
    [fixController addObject:_try];  




    _testBox = [[addObjectView alloc]initWithFrame:NSRectFromString(rectString)]; 



    NSImageView *iconView = [[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 75, 75)]; 
    testView1Controller = [[NSViewController alloc]init]; 
    testView1Controller.view = _testBox; 


    [_testBox setWantsLayer:YES]; 




    _testBox.layer.masksToBounds = YES; 
    _testBox.layer.cornerRadius = 2.0; 
    _testBox.layer.borderWidth = 2; 
    _testBox.layer.borderColor = [[NSColor darkGrayColor]CGColor]; 

    //this tells the window to add our subview testbox to it's contentview 

    [testView1Controller setView:_testBox]; 

    [testView1Controller bind:@"representedObject" toObject:fixController withKeyPath:@"content" options:nil]; 

    [mainWin addSubview:_testBox]; 
    NSTextField *obLabel = [[NSTextField alloc]initWithFrame:CGRectMake(0, 0, 50, 40)]; 
    [obLabel bind:@"value" toObject:testView1Controller withKeyPath:@"representedObject.type" options:nil]; 
    [obLabel setBackgroundColor:[NSColor clearColor]]; 
    [obLabel setSelectable:false]; 
    [obLabel setEditable:false]; 
    [obLabel setBackgroundColor:[NSColor grayColor]]; 
    [obLabel setBordered:false]; 
    [obLabel setTextColor:[NSColor whiteColor]]; 
    [iconView bind:@"value" toObject:testView1Controller withKeyPath:@"representedObject.fixImg" options:nil]; 
    //[_testBox addSubview:obLabel]; 
    [_testBox addSubview:iconView]; 

나는 새 인스턴스를 필요로하는 이유를 생성하는 방법은 그래서 속성 값을 덮어 및 각보기 내 representedObject의 데이터가 손실되지 않습니다이다.

+1

대신 NSMutableArray를 사용하고 대신에 addObject를 사용해보십시오. – Anna

+0

@Anna 그런 식으로 초기화 된 인스턴스 이름을 변경해야합니다. 그렇습니까? –

+0

당신이 당신의 질문에 당신이 성취하려는 것을 설명하거나 이것으로 해결하고자하는 문제점을 설명한다면 도움이 될 것입니다. 내 말은 : myInstances라는 NSMutableArray를 선언하십시오. 객체를 만들어서 객체에 추가하십시오. 그런 다음 색인을 사용하여 액세스 할 수 있습니다 (예 :'[myInstances objectAtIndex : 2]'). – Anna

답변

4
id object = [[NSClassFromString(@"NameofClass") alloc] init]; 
+0

클래스 이름이 아닌 클래스의 인스턴스 자체를 만듭니다. someclass처럼 * stringforclassinstance = [someclass alloc] init]; –

+2

이 코드는'NameofClass' 유형의 클래스의 인스턴스를 만듭니다. 또한 질문의 제목 줄을 읽습니다 : "NSString 사용하여 클래스의 인스턴스를 만듭니다." 이것은이 코드가하는 것입니다. 다른 의미가 있으면 질문을 수정하거나 수정하십시오. – Aaron

관련 문제