Objective-C giving value on to next View - ios

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

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.

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

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.

iOS dev - UINavigationController: send data backwards to 2nd parent view

so, I need to send data backwards when poping two views. I tried using protocol, so this is part of my code:
RootViewController.h
#import <UIKit/UIKit.h>
#import "ThirdViewController.h"
#class SecondViewController;
#interface RootViewController : UIViewController <PassInt>
#property (strong, nonatomic) SecondViewController *secondViewController;
#property (strong, nonatomic) ThirdViewController *thirdViewController;
#property (nonatomic) int intNumber;
#end
RootViewController.m
#import "RootViewController.h"
#import "SecondViewController.h"
#implementation RootViewController
#synthesize secondViewController = _secondViewController;
#synthesize thirdViewController = _thirdViewController;
#synthesize intNumber = _intNumber;
- (void)setIntNumber:(int)number{
_intNumber = number;
}
#pragma mark - View lifecycle
- (void)viewWillAppear:(BOOL)animated {
if(!_thirdViewController){
_thirdViewController = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:[NSBundle mainBundle]];
}
if(!_secondViewController){
_secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:[NSBundle mainBundle]];
self.secondViewController.thirdViewController = self.thirdViewController;
}
[self.navigationController pushViewController:self.secondViewController animated:YES];
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#class ThirdViewController;
#interface SecondViewController : UIViewController
#property (strong, nonatomic) ThirdViewController *thirdViewController;
#end
SecondViewController.m
#import "SecondViewController.h"
#import "ThirdViewController.h"
#implementation SecondViewController
#synthesize thirdViewController = _thirdViewController;
...
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
[self.navigationController pushViewController:self.userPreferencesViewController animated:YES];
}
#end
ThirdViewController.h
#import <UIKit/UIKit.h>
#protocol PassInt <NSObject>
#required
- (void) setIntNumber:(int)number;
#end
#interface ThirdViewController : UIViewController{
id <PassInt> delegate;
}
#property (retain) id delegate;
- (IBAction)saveChanges;
#end
ThirdViewController.m
#import "ThirdViewController.h"
#implementation ThirdViewController
- (IBAction)saveChanges{
int someInt = 3;
[[self delegate] setIntNumber:someInt];
UINavigationController *tempNavigationController = self.navigationController;
[tempNavigationController popViewControllerAnimated:NO];
[tempNavigationController popViewControllerAnimated:NO];
}
...
#end
I would appreciate any help!
In the UINavigationController reference check the property call :
#property(nonatomic, copy) NSArray *viewControllers
Discussion
The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.
That could give you a handle to the rootViewController.
Would that be a good solution for you?
There is other ways. But from your comment I think that could be a match.
[[self.navigationController viewControllers] objectAtIndex:0].someProerty;
Another method could be to use NSNotifications. Have the RootViewController resgister as an observer for a notification posted by the ThirdViewController. Then in the userInfo dictionary of the notification posted by the ThirdViewController, you pass someInt as an NSNumber.
This provides sufficient abstraction because your ThirdViewController doesn't need to know what kind of object requires the information, it is just posting it so interested observers know about it.
Good Luck
You're never assigning anything to the delegate member of your ThirdViewController in the code you posted. Also, delegates should usually be assign, not retain.

Resources