Add a CollectionViewController as ChildViewController to a UIView - ios

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"

Related

Push View Controller - Button Action sending from one view controller to another

I am looking to make an action from a button to another view coontroller.
When the button is pressed, the user is sent to another view controller.
However, when running the simulator and clicking the button, it is not going to the next view controller. (The next view controller is Dashboard).
Here is the .h file and .m file
`// ViewController.h
// Shopping List
//
// Created by Seenu on 2/8/16.
// Copyright © 2016. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *Login;
#property (weak, nonatomic) IBOutlet UITextField *emailTextfield;
#property (weak, nonatomic) IBOutlet UILabel *Password;
#property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
#property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)SignIn:(id)sender;
#end`//
//
// ViewController.m
// Shopping List
//
// Created by Seenu on 2/8/16.
// Copyright © 2016. All rights reserved.
//
#import "ViewController.h"
#import "Dashboard.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize button;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)SignIn:(id)sender {
Dashboard *home = [[Dashboard alloc] initWithNibName:#"Dashboard" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Make sure your view controller have navigation controller. An easy way of doing this is create navigation controller in your applicationdidFinishLaunchingWithOptions in your AppDelegate Class. Set your first view controller as rootViewController of navigationController and add it to window. Go to your AppDelegate Class and add this code over there.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIViewController *rootController =
[[RootViewController alloc]
initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
In your storyboard, just right click (or hold down control button + click) at your button then drag to the view controller you want to navigate. When option menu showing, select "Show".
Also in your storyboard, please ensure that your view controller has embedded in a UINavigationController. (Select your first view controller, in Xcode menu click Editor > Embed In > Navigation Controller).
Sorry, I wrote this answer instantly, if you need some picture, I can attach it in the next update.

Problems implementing a container view

I'm trying to follow the View Controller Programming Guide for iOS to implement a container view in my application. At the moment I'm just trying to get an initial first view to load but the label included in the first controller is not being shown. In the end, I hope to be able to control which view is shown in the container by using a segmented control.
Any help would be greatly appreciated.
My Storyboard
ViewController.h
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *contentController;
- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender;
#end
ViewController.m
#import "ViewController.h"
#import "FirstController.h"
#import "SecondController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
FirstController *firstController = [[FirstController alloc] init];
[self displayContentController:firstController];
}
- (IBAction)SegmentedControlValueChange:(UISegmentedControl *)sender
{
}
- (void)displayContentController: (UIViewController *)content
{
[self addChildViewController:content];
content.view.frame = [self frameForContentController];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}
- (CGRect)frameForContentController
{
return self.contentController.bounds;
}
#end
If FirstController is part of the storyboard, then you'll have to load it from the storyboard.
Try doing
FirstController *firstController = [self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];

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.
}

How to initialize UIViewController with existing UIView in .xib

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.

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:
___________

Resources