Sharing a UIView between UIViewControllers in a UITabBarController - iphone

I have a UIScrollView that houses a gallery of images the user can scroll through. This view needs to be visible on each of three separate UIViewControllers that are housed within a UITabBarController. Right now, I have three separate UIScrollView instances in the UITabBarController subclass, and the controller manages keeping the three synchronized (when a user scrolls the one they can see, programmatically scrolling the other two to match, etc.), which is not ideal.
I would like to know if there is a way to work with only ONE instance of the UIScrollView, but have it show up only in the UIViewController that the user is currently interacting with. This would completely eliminate all the synchronization code. Here is basically what I have now in the UITabBarController (which is where all this is currently managed):
#interface ScrollerTabBarController : UITabBarController {
FirstViewController *firstView;
SecondViewController *secondView;
ThirdViewController *thirdView;
UIScrollView *scrollerOne;
UIScrollView *scrollerTwo;
UIScrollView *scrollerThree;
}
#property (nonatomic,retain) IBOutlet FirstViewController *firstView;
#property (nonatomic,retain) IBOutlet SecondViewController *secondView;
#property (nonatomic,retain) IBOutlet ThirdViewController *thirdView;
#property (nonatomic,retain) IBOutlet UIScrollView *scrollerOne;
#property (nonatomic,retain) IBOutlet UIScrollView *scrollerTwo;
#property (nonatomic,retain) IBOutlet UIScrollView *scrollerThree;
#end
#implementation ScrollerTabBarController
- (void)layoutScroller:(UIScrollView *)scroller {}
- (void)scrollToMatch:(UIScrollView *)scroller {}
- (void)viewDidLoad {
[self layoutScroller:scrollerOne];
[self layoutScroller:scrollerTwo];
[self layoutScroller:scrollerThree];
[scrollerOne setDelegate:self];
[scrollerTwo setDelegate:self];
[scrollerThree setDelegate:self];
[firstView setGallery:scrollerOne];
[secondView setGallery:scrollerTwo];
[thirdView setGallery:scrollerThree];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self scrollToMatch:scrollView];
}
#end
The UITabBarController gets notified (as the scroll view's delegate) when the user scrolls one of the instances, and then calls methods like scrollToMatch: to sync up the other two with the user's choice.
Is there something that can be done, using a many-to-one relationship on IBOutlet or something like that, to narrow this down to one instance so I'm not having to manage three scroll views? I tried keeping a single instance and moving the pointer from one view to the next using the UITabBarControllerDelegate methods (calling setGallery:nil on the current and setGallery:scrollerOne on the next each time it changed), but the scroller never moved to the other tabs.
Thanks in advance!

Certainly you should use only one instance of your scroller view. And it will works fine without any troubles. Use method setGallery: like you did, just ensure you add your singleScrollerForAll view to view of current controller in setGallery method:
-(void)setGallery:(UIView *)aScrollerView{
[self.view addSubview:aScrollerView];
}
and call:
[firstView setGallery:singleScrollerForAll];
or
[secondView setGallery:singleScrollerForAll];
and no need to do anything in other two controllers, because when you call addSubview: the subView will be automatically removed from previous superview.

Related

iOS7 UIStatusBar blur not correct

I am using a UIToolbar for the controls at the top of the screen (There is no navigation controller) The toolbar has the look I want, however the status bar is entirely clear. I cannot seem to mimic the blur that the UIToolbar has in it's transparency. Has anyone come across a solution to this that does not involve using a navigation controller?
In Order to achieve this you need to implement methods in the UIBarPositioningDelegate protocol:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIBarPositioningDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIBarPositioningDelegate
Here is the code:
#interface ViewController : UIViewController <UIToolbarDelegate>
#property (nonatomic, weak) IBOutlet UIToolbar * toolbar;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//we become the delegate
self.toolbar.delegate = self;
}
-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar{
//this tells our bar to extend its background to the top.
return UIBarPositionTopAttached;
}
#end

iOS iPad App: Delegates not called for ViewController with two UIScrollViews and a UIPageControl (delegate functions for paging NOT CALLED)

For my iPad App, I have a main ViewController which contains two UIScrollviews and a UIPageControl.
The Problem is that the delegates for the paging are not getting called.
Here is the layout:
Selecting a button in the lower thumbScrollView needs to update the image in the mainScrollView (this works) Swiping the thumbScrollView or picking a dot on the pageControl needs to "page" the thumbScrollView to show the next previous set of buttons. The swiping does not work because the delegate functions are just not getting called.
I declare the scrollviews and pagecontrol as follows in my VC
#property (strong, nonatomic) IBOutlet UIScrollView *mainScrollView;
#property (strong, nonatomic) IBOutlet UIScrollView *thumbScrollView;
#property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
The ViewController implements UIScrollViewDelegate
#interface MyViewController : UIViewController<UIScrollViewDelegate>
And I implement the following UIScrollViewDelegate delegate functions in my VC's .m file.
- (void)scrollViewDidScroll:(UIScrollView *)sender;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
The view appears but when I swipe across the buttons I do not see the delegate functions above getting called.
I have not found a solution to this in StackOverflow although I have factored in advise from other related posts for other aspects of this (ex. the logic to distinguish which scrollview has initiated the action etc)
ADDING DETAILED CODE HERE (as requested by #HeWas)
This is the header file for the Main View Controller that controls the two scrollviews and pagecontrol (RELEVANT EXCERPTS - TELL ME IF YOU NEED MORE)
// ImageBrowseViewController.h
// (NOTE - In Interface Builder I have added a tag attribute of 0 to mainScrollView
// and 1 to thumbScrollView, to enable me to distinguish which scrollView the delegate
// needs to respond to)
#define TAG_MAIN_SCROLLVIEW 0
#define TAG_THUMB_SCROLLVIEW 1
#interface ImageBrowseViewController : UIViewController<UIScrollViewDelegate>
{
UIButton* currentlySelectedButton;
UIScrollView *mainScrollView;
UIScrollView *thumbScrollView;
UIPageControl* pageControl;
BOOL pageControlBeingUsed;
}
#property (strong, nonatomic) IBOutlet UIScrollView *mainScrollView;
// … connected as outlet in IB to mainScrollView
#property (strong, nonatomic) IBOutlet UIScrollView * thumbScrollView;
// … connected as outlet in IB to thumbScrollView
#property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
// … connected as outlet in IB to pageControl
…
-(IBAction)changePage; //Touch up Inside IBAction connected to pageControl
…
#end
This is the implementation file for the Main View Controller that controls the two scrollviews and pagecontrol (RELEVANT EXCERPTS - TELL ME IF YOU NEED MORE)
//
// ImageBrowseViewController.m
//
…
#synthesize mainScrollView;
#synthesize thumbScrollView;
#synthesize pageControl;
// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if ( [sender tag] == TAG_THUMB_SCROLLVIEW ) {
// This is the thumbScrollview
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.thumbScrollView.frame.size.width;
int page =
floor((self.thumbScrollView.contentOffset.x - pageWidth / 2) / pageWidth)
+ 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
//frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.x = self.thumbScrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.thumbScrollView.frame.size;
[self.thumbScrollView scrollRectToVisible:frame animated:YES];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed = YES;
}
You code all looks 100% (aside from this typo: #synthesize floorplanThumbScrollView;, but that isn't your problem).
I am sure that the answer is that you have not correctly wired your scrollview DELEGATES in IB.
This is the clue:
"Yes I have set all three in Interface Builder. So mainScrollView, thumbScrollView, and pageControl are wired in IB to the above declarations in the VC's .h file."
You need 2 connections between your ViewController and your scrollViews.
(1) ctrl-drag FROM viewController TO scrollView, connect to IBOutlet property.
This is what you have done.
(2) ctrl-drag FROM scrollView TO viewController, connect to delegate.
I do not think you have done this.
Explanation of step 2
UIScrollView has a built-in property called 'delegate'. The scrollView uses this property to send messages to it's delegate. You set this delegate in interface builder (step 2) or you can do it in code. For example in your viewController you could do this:
[myScrollView setDelegate:self];
which would set the viewController as the delegate for myScrollView. If you do it by linking in Interface Builder you don't need this code (and IB doesn't create any).
Either way what this actually does is set scrollView's delegate iVar to a pointer to the viewController. The great thing about using delegates like this is that the delegator (UIScrollView) doesn't have to know anything about the delegatee (in this case your UIViewController). This allows us to reuse UIScrollView so long as we observe it's delegate protocol.
Whenever the scrollView needs to notify it's delegate, internally it sends a message like this..
[self.delegate scrollViewDidScroll:self];
(you don't see that, it's in the scrollView's implementation).
The object that you have set as the delegate to scrollView needs to implement all of the required methods that the scrollView's delegate protocol declares, and can choose to implement any of the optional delegate methods. Here is the protocol
To work out which methods are required, read the UIScrollView class reference, which tells you this:
The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum (minimumZoomScale) zoom scale must be different.
Everything else in the protocol is optional.
This delegate pattern is one you can easily implement yourself for your own object reuse, and is one of the most common ways of passing messages between decoupled objects in objective-C.

Common views in viewControllers - Code re-usability

I have few common views in most of my viewControllers. What I noticed is that I can reuse single code for all viewControllers which is absolutely wise. For this I decided to create a class Utils which has static methods like
+(void)createCommonViews:(float)yAxis:(NSString*)text;
In my case common views are three labels and two images.
Problem : I am not able to add these views from Utils. I am wondering how can I send self as a parameter so that I may add the views from Utils. It may be wrong to add views outside the viewController. In that case what can be the solution? Taking all these views in a UIView, setting return type of Utils method as UIView and then adding UIView to viewController (after calling method from viewController) might solve my problem. But what I am looking for is some other solution.
+(void) createCommonViews:(float)yAxis withText:(NSString*) text toTarget:(UIViewController*) target
{
//create views
[target addSuview:view];
}
But I think returning a Uiview and then adding it in the UIViewController afterwards, is a far better solution.
The method you're attempting is to have your view object as a singleton. This is uncommon at best, at worst a crash waiting to happen. Better design is for each of your view controller classes to have its own instance of your custom view, like so:
#interface MyCommonView : UIView
// ...
#end
#interface MyViewController_A : UIViewController {
MyCommonView *commonView;
}
#property (strong, nonatomic) IBOutlet MyCommonView *commonView;
#end
// Meanwhile somewhere else...
#interface MyViewController_B : UIViewController {
MyCommonView *commonView;
}
#property (strong, nonatomic) IBOutlet MyCommonView *commonView;
#end
Create a viewController that acts as a parent view for all your common stuff, call it CommonViewController then implement this in all the viewcontrollers you want it to appear
-(void) viewDidLoad
{
[self.view addSubView:[[CommonViewController alloc] initWithRect:..];
}
Or alternatively using xib files

UIView Buttons loaded with XIB do not work

I've loaded a UIView (FirstView.m) with a separate XIB (SecondView.xib), but the buttons in that XIB crash the app. The code for the buttons (IBOutlet & IBAction) are in SecondView.m.
Do I need to point the code from SecondView.m to FirstView.m? I tried using #import and #class... but was unsuccessful.
The code I'm using is completely valid... I'm pretty sure the issue has something to do with the XIB being loaded into the UIView... and then possibly losing its connection to the implementation file. Thoughts?
Thanks in advance!
FirstView.h
#import <UIKit/UIKit.h>
#interface FirstView : UIViewController {
IBOutlet UIView *SecondViewPopUP;
IBOutlet UIButton *openBTN;
}
#property (nonatomic, retain) UIView *SecondViewPopUP;
#property (nonatomic, retain) IBOutlet UIButton *openBTN;
-(IBAction)showPopUp:(id)sender;
FirstView.m
#synthesize SecondViewPopUP;
#synthesize openBTN
- (void)viewDidLoad {
SecondViewPopUP.alpha = 0;
// Add IncidentsViewController to view
SecondView *SecondV=[[SecondView alloc] init];
SecondV.view.frame = CGRectMake(0, 0, 262, 269);
SecondV.view.clipsToBounds = YES;
[SecondViewPopUP addSubview:SecondV.view];
SecondViewPopUP.frame = CGRectMake(0, 76, 262, 269);
[SecondV release];
}
-(IBAction)showPopUp:(id)sender {
NSLog(#"Stats Button was pressed");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
SecondViewPopUP.alpha = 1;
[UIView commitAnimations];
}
SecondView.h
#import <UIKit/UIKit.h>
#interface ShareViewController : UIViewController {
IBOutlet UIButton *share_facebook;
IBOutlet UIButton *share_twitter;
}
#property (nonatomic, retain) IBOutlet UIButton *share_facebook;
#property (nonatomic, retain) IBOutlet UIButton *share_twitter;
-(IBAction)shareOnFB:(id)sender;
-(IBAction)shareOnTwitter:(id)sender;
SecondView.m
#synthesize share_twitter, share_facebook;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(IBAction)shareOnFB:(id)sender {
NSLog(#"Shared on FB");
}
-(IBAction)shareOnTwitter:(id)sender {
NSLog(#"Shared on Twitter");
}
First of all FirstView (and presumably SecondView) is a UIViewController not a UIView so naming it "FirstViewController" would be much clearer. Views and view controllers are very different things.
Secondly you are adding a UIViewController's view as a subview of another view on the line "[SecondViewPopUP addSubview:SecondV.view];" That's not how UIViewControllers are expected to be used and the UIViewController programming guide recommends against it for good reason.
Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. In iPhone applications, the views in a view hierarchy traditionally cover the entire screen, but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.
Finally if you were to post the error listed when your app crashes we would probably see that you are attempting to send -shareOnFB: or -shareOnTwitter: messages to an instance of "FirstView" which does not implement them because your nib bindings are not configured appropriately ie you set the File's Owner of the nib to be "SecondView" and then loaded it with an instance of "FirstView" as its owner. Impossible to say for sure without more data.

One UIViewController with many UIViews

I'm creating an app with one UIViewController and many UIViews. I have MainViewController with a UIView underneath it that displays when loaded up and a few other UIViews all in the MainWindow.xib. How do I go about switching from one View to the next?
Update:
Thanks for the reply.
I have added MainViewController to my appDelegate.
On FinishedLaunching: [window addSubview:[viewController view]];
That View Controller has a function in it called goToNextPage.
-(IBAction)goToNextPage:(id)sender{
[self.view removeFromSuperview];
[self.view addSubview:tableOfContents];
}
In Interface Builder I have that View Controller added to MainWindow.xib. Under that View Controller I have a UIView (called Cover) that loads as it's child on startup and another UIView (eventually many) named TableOfContents that is on its own.
I tried to post an image of my MainWindow.xib here but apparently my Reputation isn't high enough.
The UIView Cover has a button on it that is linked to the goToNextPage function.
When I hit the button the page goes blank as if the one view is successfully being removed but the next is not being loaded.
How do I get the goToNextPage function to switch the preloaded UIView Cover with the other UIView called TableOfContents?
Another Edit:
BookTest6AppDelegate.h:
#import <UIKit/UIKit.h>
#class MainViewController;
#interface BookTest6AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet MainViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet MainViewController *viewController;
#end
BookTest6AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[window addSubview:[viewController view]];
[window makeKeyAndVisible];
return YES;
}
MainViewController.h
#import <UIKit/UIKit.h>
#class TableOfContents;
#interface MainViewController : UIViewController {
IBOutlet TableOfContents *tableOfContents;
}
#property (nonatomic, retain) IBOutlet TableOfContents *tableOfContents;
-(IBAction)fGoToTableOfContentsController:(id)sender;
-(IBAction)fGoToNextPageController:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "TableOfContents.h"
#implementation MainViewController
#synthesize tableOfContents;
-(IBAction)fGoToTableOfContentsController:(id)sender{
[self.view removeFromSuperview];
[self.view addSubview:self.tableOfContents];
}
-(IBAction)fGoToNextPageController:(id)sender{
}
My UIView Classes basically just initiate buttons that link to the functions defined in MainViewController.h.
First you need to know when switching from one to another.
Then you say it to your MainController and addSubView to the mainController.view an another view.
Maybe if you give us more informations or code we could help you a little more :-)
Good Luck
Vincent
Edit :
[self.view removeFromSuperview];
Here you are removing the window view. I think it's not the better way ^^ Try
[ viewController.view removeFromSuperview ]; :-)
Does it work better ?
Edit :
Edit bis : han, it's my fault. You need to remove controller and add another one in the app delegate and not in your mainController (you could do it too, but no with your actual code).
You have two options :
1°) Declare all your controllers in your app delegate.
2°) Declare all your controllers in one main controller. But the main controller would be here to "control" others one.