简体   繁体   中英

Objective-C Create an instance of ViewController

I wanted to create an instance of the ViewController class with :

ViewController *viewConnection = [[ViewController alloc]init];

self.image.center = CGPointMake(self.image.center.x + 1, self.image.center.y);

if (CGRectIntersectsRect(self.image.frame, viewConnection.otherImage.frame)) {
    [self.movementTimer invalidate];
}`

it doesn't enters the if-statement when the image in the class hits the image in the ViewController , can somebody tell me why ?

Try creating an shared instance of your ViewController's class like the following:

+ (id)sharedInstance {

    static ClassName * sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];

        // Do stuff

    });
    return sharedInstance;

}

Your should use @property and @synthesize to access the variables and outlets while initializing your ViewController .

Example :

In .h file

@property (nonatomic, retain) IBOutlet UIView * testView;

In .m file

@synthesize testView;

Thanks!

To access the variables and outlets you have to define the propreties in .h file:

@interface ViewController : UIViewController
//Property
@property (nonatomic, copy) NSString *myString;
//Outlet
@property (nonatomic, weak) IBOutlet UILabel *myLabel;
@end

After that you can create object and access it properties:

ViewController *viewConnection = [[ViewController alloc]init];
viewConnection.myString = @"string";
viewConnection.myLabel.text = @"label text";

//Extended

In your example you create viewConnection object and after that you check it properties' frame:

viewConnection.otherImage.frame

but you haven't set it up, you haven't set up otherImage, you haven't set up it's frame so this is why you have the issue.

// Extended 2

The reason why it's doesn't work is because you try to change frame of the property (otherImage), I assume it's IBOutlet just after you initialised the view controller but the IBOutlets are created later in view controller lifecycle, you should chance the outlets in viewDidLoad or viewDidAppeare but this method fired after you present/push the view controller. So you should create variable, let's say CGRect and after you initialise view controller set up it value, after that push/present the view controller and in your class (ViewController), for example, in viewDidLoad set up otherImage.frame = variable.

Below method checks if two frames intersect. This works fine if the origins of the ViewControllers are identical!

CGRectIntersectsRect(self.image.frame, viewConnection.otherImage.frame)

If your "viewConnection" has a position of the 100px top and 100px left then CGRectIntersectsRect will not know about that and could just return 'false' although the views may overlapping visually . That does not necessarily mean that the frames are intersecting.

That said you will need to convert the rect of your "otherImage" when you test for an intersection:

if( CGRectIntersectsRect([viewConnection.otherImage convertRect:viewConnection.otherImage toView:self.view] , self.image.frame) ){
    NSLog(@"INTERSECTION");
}

Just after creation UIViewController object does not have view , it should be loaded later, so to make your code work call [viewConnection view] for example to load view, and after that check frames

ViewController *viewConnection = [[ViewController alloc]init];

[viewConnection view];

self.image.center = CGPointMake(self.image.center.x + 1, self.image.center.y);

if (CGRectIntersectsRect(self.image.frame, viewConnection.otherImage.frame)) {
    [self.movementTimer invalidate];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM