1
votes

I am facing problem updating UIView asynchronously with device orientation in place. I have implemented device orientation in viewDidload as below

- (void)viewDidLoad{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
[self initialize];}

In orientationChanged method, I have following code

-(void)orientationChanged {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;

if(UIInterfaceOrientationIsLandscape(orientation)){
    UINib *nib = [UINib nibWithNibName:@"ConsoleViewControllerLandscape" bundle:nil];
    UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
    self.view = portraitView;

    [self initialize];

} else {
    UINib *nib = [UINib nibWithNibName:@"ConsoleViewController" bundle:nil];
    UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
    self.view = portraitView;

    [self initialize];

}

In initialize method, I actually update UI asynchronously with codes like

 [self performSelectorOnMainThread:@selector(arrangeAsynchronously) withObject:nil waitUntilDone:NO];
- (void) arrangeAsynchronously{
    //Some complex calculation and finally
[self.view addSubview:imageview];
}

The problem is when orientation changed imageViews are not added to main view. Lets say I am starting with portrait view then I can see all imageviews in portrait view and if it changed to landscape, then view is blank. Again if I switched to portrait, then all subviews i.e. imageViews are properly added. The problem is when orientation changed, I am loading a new nib file however the code still refers to old view loaded from old nob file. How can I change reference. This problem only occurs when I do in asynchronous mode.

Its not problem with uiview rather its with calculation of subview positions after device rotation. Earlier my code was

CGAffineTransform inverseTransform = CGAffineTransformInvert(self.view.transform);
fixedPoint = CGPointApplyAffineTransform(fixedPoint,inverseTransform);
fixedPoint = CGPointMake(fixedPoint.x+126, fixedPoint.y-109);

And I changed it to

fixedPoint = CGPointMake(fixedPoint.x+126, fixedPoint.y-109);

But still I am clueless why affinetransform does not work waitUntilDone:NO and works in waitUntilDone:YES.

1

1 Answers

0
votes

Your strategy is a bit problematic. self.view is presented somehow, and just because you make a new UIView, doesn't mean it's get replaced in the presented window.

I suggest that you have a container view added to your main UIView (the one you replace here), and then change the content of the container view when device orientation is changed.

EDIT

My answer might seem a bit unclear so let me try to explain it more detailed. the UIView of a UIViewController is presented by a UIWindow. It means that the window has a reference to the view. When you change the UIView of a UIViewController (by constructing a new UIView) it does NOT mean that it will reflect in the UIWindow.


Initial scenario:

UIViewController->UIView1<-UIWindow

After you construct a new UIView:

UIViewController->UIView2
UIWindow->UIView1


As you can see on the above figure, you don't gen any errors, but you now have two views, and changes made in your UIViewController does not reflect on the screen.

You might be so lucky that it works the first time however: If you reconstruct the UIView BEFORE it is presented by the UIWindow everything still works, but it is still a bad design, that might easily break (like you are seeing after the timing is changed).

This is the case where things might actually work:


Initial scenario:

*UIViewController->UIView1

After you construct a new UIView:

*UIViewController->UIView2

the window present the view on screen:

UIViewController->UIView2<-UIWindow


Basically you should NEVER throw away your handle to the UIView of a UIViewController after it has been presented.

I hope this helps on your understanding of how referencing/pointers works.