How to initialize UIViewController with existing UIView in .xib - ios

Imagine a UIViewController that is initialized with a .xib file. Now imagine that this .xib contains a subview which is a UICollectionView object.
How would one initialize a UICollectionViewController instance using this UICollectionView instance?
I have tried:
#property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
- (void)viewDidLoad
{
[super viewDidLoad];
MyUICollectionViewControllerSubclass *controller = [[MyUICollectionViewControllerSubclass alloc] initWithNibName:nil bundle:nil]; //no matching .xib
controller.collectionView = self.collectionView;
[self addChildViewController:controller]; [controller didMoveToParentViewController:self];
}
But for some reason MyUICollectionViewControllerSubclass is never even firing viewDidLoad or loadView.

UICollectionViewController has:
#property (nonatomic, retain) UICollectionView *collectionView;
So, after you create your instance you can explicitly set the collectionView property to the subview (that you need to get a reference to either (preferably) from an outlet / tag / subview iteration.

Related

Objective-C giving value on to next View

#import "OtherController.h"
#import "ViewController.h"
ViewController *viewController;
#interface OtherController ()
#end
#implementation OtherController
- (void)viewDidLoad
{
[super viewDidLoad];
viewController = [[ViewController alloc] initWithNibName:#"viewController" bundle:nil];
}
- (IBAction)levelNormal{
viewController.level = 1;
[self.navigationController pushViewController:viewController animated:YES];
}
I have tried various tips i read on stackoverflow though none of them seemed to work out correctly. I am trying to open a View on Button-click and when the View opens, depending on which button was pressed the View should have a different "level" number. I do not have a NavigationController as far as i'm concerned. I have read different posts about pasting code into the AppDelegate class, though that also did not work out for me. When I debug the pressing of the button, I see that the value is changed but nothing else happens when it tries to change the view.
Here's the ViewController header-file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *resumeButton;
#property (weak, nonatomic) IBOutlet UIImageView *opacityScreen;
#property (weak, nonatomic) IBOutlet UILabel *scoreField;
#property (weak, nonatomic) IBOutlet UIButton *pauseButton;
#property (weak, nonatomic) IBOutlet UIButton *mainMenuButton;
#property (weak, nonatomic) NSTimer * nst;
#property(nonatomic) int level;
#end
You have to init UINavigationController with root view controller OtherController.
Check this tutorial:
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
Create UINavigationController object in AppDelegate's didFinishLaunchingWithOptions and assign a UIViewcontroller object as a rootViewController like below and pass the navObj to window root view.
viewController = [[ViewController_iPhone alloc] initWithNibName:#"ViewController_iPhone" bundle:nil];
UINavigationController *navObj = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.window setRootViewController:navObj];
- (IBAction)levelNormal
{
// viewController.level = 1;
OtherController *nextobj=[[OtherController alloc] initWithNibName:#"OtherController" bundle:nil];
nextobj.dictionay_passdata=[your_array_data]; //your data
[self.navigationController pushViewController:nextobj animated:YES];
}
In your OtherController make a dictionary for store data in OtherController
OtherController.h file
{
NSMutableDictionary *dictionay_passdata;
}
OtherController.m file
#implementation OtherController
#synthesize dictionay_passdata; //this is must to write in your code
- (void)viewDidLoad
{
NSLog(#"%#",dictionay_passdata);
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

iOS use of custom Container View Controller

I try to use an UIViewController as an custom ContainerViewController to show certain ViewControllers with custom behavior. I have an UIView "detailsView" property in my ContainerViewController to show the content of the loaded ViewControllers. The problem is that the loaded controller is shown but without any content like buttons. What do I do wrong?
part of my ContainerViewContoller.h:
#property (weak, nonatomic) IBOutlet UIView *detailsView;
#property (strong, nonatomic) UIViewController *currentViewController;
the viewDidLoad method of my ContainerViewContoller.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"called");
SWResultViewController *swResultViewController = [[SWResultViewController alloc]init];
self.currentViewController = swResultViewController;
// this line doesn't help to show the buttons
swResultViewController.view.frame = self.detailsView.bounds;
[self addChildViewController: self.currentViewController];
[self.detailsView addSubview: self.currentViewController.view];
[self.currentViewController didMoveToParentViewController:self];
}
should be shown:____________________________________is shown:
___________

Adding and removing children ViewControllers to UIViewController when ToolBar Button is pressed

I am implementing a UIViewController with ScrollView. In the centre of the view I have a ToolBar like that one in the picture:
I have four UIViewControllers to add, one for each button of the toolBar. I do not know if I should init all off them at the beginning and then with a NSArray of viewControllers and one NSArray of booleans manage all of them with this methods:
How could I manage this?? Change viewControllers at the bottom of the toolBar while any button is pressed
- (void) displayContentController: (UIViewController*) content;
{
scrollView.contentSize =CGSizeMake(scrollView.frame.size.width, self.view.frame.size.height + content.view.frame.size.height );
[self addChildViewController:content];
content.view.frame = [self frameForContentController];
[scrollView addSubview:content.view];
[content didMoveToParentViewController:self];
}
- (void) hideContentController: (UIViewController*) content
{
[content willMoveToParentViewController:nil]; // 1
[content.view removeFromSuperview]; // 2
[content removeFromParentViewController]; // 3
}
I have never used childViewControllers actually and I really do not know how to use them
You want to essentially create your own TabBarcontroller. You should use child view controller otherwise auto rotation will not work as expected. You should use an array of ViewControllers (required to pass data) and delegates when your interact with your tabBar. Here is an excellent example which does the same.MHTabBarController.
Here is a sample interface for it:
#interface MHTabBarController : UIViewController
#property (nonatomic, copy) NSArray *viewControllers;
#property (nonatomic, weak) UIViewController *selectedViewController;
#property (nonatomic, assign) NSUInteger selectedIndex;
#property (nonatomic, weak) id delegate;
- (void)setSelectedIndex:(NSUInteger)index animated:(BOOL)animated;
- (void)setSelectedViewController:(UIViewController *)viewController animated:(BOOL)animated;
#end

Add a CollectionViewController as ChildViewController to a UIView

I am trying to create a view which contains three subview having different view controller (UICollectionViewController,pageviewcontroller and uiviewcontroller).
i am able to add a uiviewcontroller but other two controller is not allowed.
I am getting this error.....
Incompatible pointer types sending 'UICollectionView *__weak' to parameter of type 'UIViewController *'
Is their any way to add these controller to my subview?
I don't know why you want to add ViewControllers inside a view, i never need it.
I tried to do that, if can help you this is my running code:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, retain) UICollectionViewController *collectionViewController;
#property (nonatomic, retain) UIPageViewController *pageViewController;
#property (nonatomic, retain) UIViewController *simpleViewController;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize collectionViewController;
#synthesize pageViewController;
#synthesize simpleViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
collectionViewController = [[UICollectionViewController alloc] initWithCollectionViewLayout:layout];
pageViewController = [[UIPageViewController alloc] init];
simpleViewController = [[UIViewController alloc] init];
// Do your stuff with this controllers
[self.view addSubview:collectionViewController.view];
[self.view addSubview:pageViewController.view];
[self.view addSubview:simpleViewController.view];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You don't add view controllers to views. You add views to views (as subviews) and, much more rarely, controllers to controllers (as child controllers).
Think of them as two parallel hierarchies: Given ControllerA that controls view ViewA, you want to make ControllerB a child controller of ControllerA such that it's view, ViewB is a subview of ViewA.
See the WWDC 2011 video: "Implementing UIViewController Containment"

Passing data to the ViewController issue

My task is to set background image in UIImageView in SecondViewController by clicking button on the FirstViewController.
FirstViewController.h :
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#class SecondViewController;
#interface ViewController : UIViewController
{
SecondViewController *secondViewData;
}
// pointer to second view
#property (nonatomic, strong) SecondViewController *secondViewData;
// buttons
- (IBAction)changeBack:(id)sender;
#end
button method in FirstViewController.m :
- (IBAction)changeBack:(id)sender {
SecondViewController *svc = [[SecondViewController alloc] init];
self.secondViewData = svc;
svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"]];
svc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:svc animated:YES completion:nil];
}
SecondViewController.h :
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#interface SecondViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *back;
#end
back - it's a IBOutlet UIImageView in SecondViewController.
Also I imported FirstViewController.h in SecondViewController.m.
I tried to pass string to SecondViewController - and it's logged fine in -ViewDidLoad in SecondViewController, but can't set UIImageView.
First of all, read Resource Management in View Controllers
I thought, the problem is that first you set UIImageView through property, and then its set from xib, when view loaded in modal controller.
This line is the problem:
svc.back = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"]];
You're setting back to a newly allocated image view, rather than the one that it's hooked up to in IB. Just change that line to:
svc.back.image = [UIImage imageNamed:#"1.png"]];
You can do something like this.
You can have an an outlet of UIImageView in your secondviewcontroller.h
IBOutlet UIImageView *imgView;
also property and synthesize this
#property(nonatomic, retain) IBOutlet UIImageView *imgView;
connect the outlet iof secondViewController in the FirstViewController.xib
In your firstviewcontrollem.m implement this where you want to set the image in the secondViewController UIImageView
self.secondViewData.imgView.image = [UIImage imageNamed:#"1.png"];
U need to take another imageview at second view controller.I named as imgView.Let original image view as bgView.
enter code here
//At secondviewcontroller.h
#property(nonatomic,strong)IBOutlet UIImageView *bgView;
#property(nonatomic,strong)IBOutlet UIImageView *imgView;
//At secondviewcontroller.m
//At viewDidLoad
self.bgView.image=self.imgView.image;
//At firstviewcontroller.m
//button action should be like this
-(IBAction)buttonPressed:(id)sender
{
SecondViewController *secondVC=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
secondVC.imgView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"hue.png"]];
[self presentModalViewController:secondVC animated:YES];
}
Try this.

Resources