Splitting a view into two
I am not very familiar with the vocabulary used, but I will do my best to explain what I am trying to do.
I would like to create an iPad application which has a Navigation Controller as the root controller (yes, I am aware this goes against Apples recommendations for creating a flattened hierarchy). When the app loads, the user will enter some information and then press a "Next" button. I know how to create the create the navigation controller, it is this next page I am having a difficult time with.
On this next page, I am trying to create a view that is divided in half (NOT a SplitViewController). On the left there is a view controller, and on the right there is ano开发者_运维知识库ther view controller (both independent of each other). Similar to a website having two iframes.
Can someone please point me in the right direction? I'm really not even sure what to search for.
In cases where UISplitViewController won't cut it or you can't use it because you're on the iPhone, there are a number of different ways to do this:
- Create a parentController that alloc/inits two child viewControllers and then adds their views as subviews. This is the approach I use when I need to, but I should warn that Apple advises against it in the Note section of the View Controller Programming Guide
- Create a parentController that is responsible for say the left side but it alloc/inits a child viewController that is responsible for the right side and adds its view as a subview. Same caveat as above, Apple advises against it.
- Similar to the above, but use the Apple recommended approach of having your sub controllers subclass NSObject instead of UIViewController. Here's a good blog post on the subject.
- Jam it all together into one monster viewController that is responsible for everything. This is the most common approach you'll find used if you're inheriting old iOS code (and in my opinion the worst).
I personally prefer the first, second, or third approaches as it leads to better separation of concerns, good encapsulation, and therefor cleaner and easier to maintain code. I also find that later on if we change the flows to move away from a split style view to two different screens, the individual viewControllers are completely re-usable because they are well encapsulated.
I made TUSplitSyncScrollView like below images.
In this code, I used KVO for observing another window's behavior as below:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
if (keyPath == @"observedPresentScale") {
// 自身のzoomScaleに監視しているobservedPresentScaleの値を代入する
// まず拡大縮小可能にする
self.maximumZoomScale = 1000.0;
self.minimumZoomScale = 0.001;
self.zoomScale = [change[@"new"] floatValue];
// ここで拡大縮小禁止にする
self.maximumZoomScale = self.zoomScale;
self.minimumZoomScale = self.zoomScale;
printf("zoomScale = %f\n", self.zoomScale);
}
else if (keyPath == @"observedContentOffsetY") {
self.contentOffset = CGPointMake(self.contentOffset.x, [change[@"new"] floatValue]);
}
}
And both two views observe each other.
int opt = NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew;
// rightSplitedScrollViewをleftSplitedScrollViewのオブザーバにする
[leftSplitedScrollView addObserver:rightSplitedScrollView forKeyPath:@"observedPresentScale" options:opt context:NULL];
[leftSplitedScrollView addObserver:rightSplitedScrollView forKeyPath:@"observedContentOffsetY" options:opt context:NULL];
// leftSplitedScrollViewをrightSplitedScrollViewのオブザーバにする
[rightSplitedScrollView addObserver:leftSplitedScrollView forKeyPath:@"observedContentOffsetY" options:opt context:NULL];
// [rightSplitedScrollView addObserver:leftSplitedScrollView forKeyPath:@"observedPresentScale" options:opt context:NULL];
You can run the actual project from here:https://github.com/weed/TUSplitSyncScrollView
精彩评论