Can I check if the user interface element has a specific reference socket?

I am writing unit tests to find if elements exist on a specific nib. For example, I want to scroll through nibs views and check if 'commentTextView' exists with referenced output to check if this text view exists.

Right now, I see only methods to check if a target exists (for example, check if a button will trigger a specific selector when pressed), but not check what I need.

+5
source share
2 answers

Instead of checking to see if they have a power outlet, assign them all unique tags. Select the buttons in Interface Builder and go to the Attribute Inspector on the right. To the bottom there should be a field for setting the property of the object tag. Then, while you are viewing your nib views, you can check each tag and use it to determine what kind it looks like.

0
source

I make a few assumptions here, so if I am in the database let me know.

1) You have a list of objects that will be connected to outlets, and a list of these points. (e.g., file owner is a class MyViewControllerand has exits view, label, buttonetc. have UITableViewthe outputs delegateand dataSourceetc.).

2) , 1. , - UIControl -, viewWithTag:

, , , , ( )

for each referencingObject in nibObjects
{
    for each outletName in referencingObject.outletNames
    {
        assertExistence(/* is an object referenced by this outlet? */)
        assertProperties(/* does the object conform to the properties expected for this referencing object / outlet pairing? */)
    }
}

. iOS nibs , , , . nib, , , .

, SenTestCase:

ViewController *vc = [[ViewController alloc] init];
UINib *nib1 = [UINib nibWithNibName:@"ViewController1" bundle:nil];
NSArray *topLevelObjects = [nib1 instantiateWithOwner:vc options:nil];

ReferencingObject *filesOwnerReferencingObject = [[ReferencingObject alloc] init];
filesOwnerReferencingObject.object = vc;

//Make a referenced object outlet for the view
ReferencedOutlet *viewOutlet = [[ReferencedOutlet alloc] init];
viewOutlet.name = @"view";
viewOutlet.propertyAssertionBlock = ^(id object) {
    UIView *theView = (UIView *)object;
    STAssertEquals(1.0f, theView.alpha, @"shouldn't have any transparency");
};

//Make a referenced object outlet for the label
ReferencedOutlet *labelOutlet = [[ReferencedOutlet alloc] init];
labelOutlet.name = @"label";
labelOutlet.propertyAssertionBlock = ^(id object) {
    UILabel *theLabel = (UILabel *)object;
    NSString *expectedLabelText = @"ViewController1.xib";
    STAssertTrue([expectedLabelText isEqualToString:theLabel.text], nil);

};

filesOwnerReferencingObject.outlets = @[ viewOutlet, labelOutlet ];


NSArray *referencingObjects = @[ filesOwnerReferencingObject ];
for (ReferencingObject *referencingObject in referencingObjects)
{
    for (ReferencedOutlet *outlet in referencingObject.outlets)
    {
        id object = [filesOwnerReferencingObject.object valueForKey:outlet.name];
        STAssertNotNil(object, nil);
        outlet.propertyAssertionBlock(object);
    }
}

/ ReferencingObject ReferencedOutlet.

@interface ReferencingObject : NSObject

@property (nonatomic, strong) id object;
@property (nonatomic, strong) NSArray *outlets;

@end

@implementation ReferencingObject
@end

typedef void (^ReferencedOutletPropertyAssertionBlock)(id);

@interface ReferencedOutlet : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) ReferencedOutletPropertyAssertionBlock propertyAssertionBlock;

@end

@implementation ReferencedOutlet
@end

, - . , .

0

All Articles