Passing data to the ViewController issue - ios

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.

Related

label is not updating when using a delegate method

I have two viewController and iam using a delegate method to update a label in the first view controller with text entered in the second viewcontroller.
My problem is the value entered in the textfield(secondViewController) is not getting pushed into the UILabel(FirstVIewController)
My First ViewController.h
#import "SecondViewController.h"
#interface ViewController : UIViewController <CustomDelegate>
#end
First ViewController.m
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *infoLabel;
#end
#implementation ViewController
-(void)sendinfo:(NSString *)info
{
_infoLabel.text = info;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"info"])
{
SecondViewController *obj = segue.destinationViewController;
obj.userDelegate = self;
}
}
SecondViewController.h
#protocol CustomDelegate <NSObject>
-(void)sendinfo:(NSString *)info;
#end
#interface SecondViewController : UIViewController
#property (weak, nonatomic) id <CustomDelegate> userDelegate;
#property (weak, nonatomic) IBOutlet UITextField *textField;
SecondViewController.m
- (IBAction)sendInfo:(id)sender {
[self.userDelegate sendinfo:_textField.text];
UIViewController * controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Home"];
[self presentViewController:controller animated:YES completion:Nil];
}
You are creating another UIViewController in your SecondViewController and the delegate is set to the viewController from where you are coming.
To see your changes, you should do a [self dismissViewControllerAnimated:YES completion:nil]; in your SecondViewController
Example:
- (IBAction)sendInfo:(id)sender {
[self.userDelegate sendinfo:_textField.text];
[self dismissViewControllerAnimated:YES completion:nil];
}

Assigning image from another View Controller class

I have a unassigned UIImageView in one View Controller class. I want to assign it using UIImage ImageNamed:#"examplePic.png" from another View Controller class's .m file
Here is my code -
Class1.h
//The class with the unassigned UIImageView
#property (strong, nonatomic) IBOutlet UIImageView *myImage;
//The UIImageView is connected to the #property
Class2.m
//
Class1 *class1 = [[Class1 alloc] init];
class1.myImage.image = [UIImage imageNamed:#"examplePic.png"];
It looks like your UIImageView is not being initialized before you attempt to set its image property. Try the following:
class1.myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"examplePic.png"]]
The problem is that your UIImageView has not been initialised yet. To fix this:
Add a new property for an UIImage:
#property (nonatomic) UIImage *imageForImageView;
When initialising your UIViewController which will be pushed onto the stack try this:
UIViewController *someViewController = [[UIViewController alloc] initWithNibName:#"SomeNibName" bundle:nil];
someViewController.imageForImageView = image;
[self.navigationController pushViewController:someViewController animated:YES];
Then in your viewDidLoad of someViewController you can assign imageForImageView as image to your UIImageView.
This way you ensure that the UIImageView actually is in memory when assigning an UIImage to it.

Access UINavigationViewController from UIImageView class

I have a UIImageView with a custom class. When the image is pressed the class should push a new view controller, however I don't seem to be able to get a reference to the current UINavigationController. I would usually do self.navigationController, but here I can't.
I tried the following code but it doesn't work:
[self.window.rootViewController.navigationController pushViewController:browser animated:YES];
Give your Custom Image View a delegate:
#protocol CustomImageViewDelegate;
#interface CustomImageView : UIImageView
#property (weak, nonatomic) id<CustomImageViewDelegate> delegate;
#end
#protocol CustomImageViewDelegate
- (void)imageViewPressed:(CustomImageView *)imageView
#end
Make you view controller the delegate. When the image is pressed, you do [self.delegate imageViewPressed:self.
Your view controller will have the implementation:
- (void)imageViewPressed:(CustomImageView *)imageView
{
[self.navigationController pushViewController:browser animated:YES];
}
Or give your view a Navigation Controller property:
#interface CustomImageView : UIImageView
#property (weak, nonatomic) UINavigationController navigationController;
#end
Then you can use [self.navigationController pushViewController:browser animated:YES] directly on your Custom Image View.
You'll have to set the navigationController property, iOS won't do it for you.
On your controller add the line
self.yourCustomImageViewProperty.navigationController = self.navigationController;
This is a very basic response, but I hope it steers you in the right direction.
First make UIImageView to UserInteractionEnabled = YES.
Check that self.window.rootViewController.navigationController is
nil or not .
If the root view controller is already a navigation controller, then you could do this:
UINavigationController *nav = self.window.rootViewController;
[nav pushViewController:browser animated:YES];

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

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"

Resources