I'm new to iOS and Its developing.i have read Xcode old style passing data between view controllers.there i have seen lot of things going with xib files.please find below the code i have used to pass data in-between ViewControllers.but there in customelink(please find DetailViewController.m)there I'm getting Null value.please help me to get String value there.and kind enough to explain what are the mistakes I've done here.
EssentialInfoController.h
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
#interface Essentialinfocontroller : UIViewController
#end
EssentialInfoController.m
#import "Essentialinfocontroller.h"
#interface Essentialinfocontroller ()
#end
#implementation Essentialinfocontroller
- (void)viewDidLoad
{
[super viewDidLoad];
DetailViewController * customelink= [DetailViewController alloc ];
customelink.link=#"https://www.facebook.com";
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property(weak,nonatomic)NSString * link;
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#property(nonatomic,weak)NSString *customelink;
#end
#implementation DetailViewController
#synthesize link;
#synthesize customelink;
- (void)viewDidLoad
{
[super viewDidLoad];
self.customelink=self.link;
NSLog(#"link--> %#",customelink);// here 2014-07-06 21:21:51.469 WADTourisum[880:60b] link--> (null)
}
#end
DetailViewController * customelink= [DetailViewController alloc ];
should be
DetailViewController * customelink= [[DetailViewController alloc ] init];
and
#property(weak,nonatomic)NSString * link;
should be
#property(strong,nonatomic)NSString * link;
and log it
NSLog(#"link--> %#",self.link);
you don't need this one
#property(nonatomic,weak)NSString *customelink;
if you use storyboard and segue then you have to implement
-(void)prepareForSegue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
assume that you got a button to move from essentialVC to detailVC, in you IBAction, call this method:
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME_HERE" sender:self];
this is just a brief answer. I suggest you read about UINavigationController first, it's much easier to use compared to segue, IMHO.
Related
I have two view controllers in my project ViewController and SecondViewController and my ViewController has a button which performs a popover segue to SecondViewController then in my SecondViewController I have a button which I would like to use to a call a method from ViewController and I assumed it was possible using [self presentingViewController] but that pointer is set to nil so I'm not quite sure how to call a method in the first ViewController inside the SecondViewController after a popover segue.
This is the code for my ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)dosegue:(id)sender;
-(void)tobecalledfromseguepopover;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
}
-(void)tobecalledfromseguepopover{
NSLog(#"Called from segue popover");
}
- (IBAction)dosegue:(id)sender {
[self performSegueWithIdentifier:#"segueforpopup" sender:self];
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface SecondViewController : UIViewController
- (IBAction)returntocaller:(id)sender;
#end
SecondViewController.m
#import "SecondViewController.h"
#interface SecondViewController()
#end
#implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)returntocaller:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
ViewController* caller = (ViewController*)[self presentingViewController];
if(caller!=nil){
[caller tobecalledfromseguepopover];
}else{
NSLog(#"nil");
}
}];
}
#end
My UI
So any ideas on how I can call tobecalledfromseguepopover inside SecondViewController after the segue? Also is there a way for SecondViewCOntroller to know it's being called from a segue popover?
The usual thing is a delegate architecture. Give the popover a delegate property and set it to self in your prepareForSegue. Now the popover knows where to find the first view controller.
This is often combined with a protocol so that the popover knows the name of the method to be called without worrying about the class of the first view controller, but that part isn’t crucial, especially in Objective C.
I'd like to trigger a message from viewController to another viewController.
And I coded like below, but viewController didn't call delegate.
I want to call delegate without stroyboard. I just want to send a message to another viewController.
viewController2.h
#protocol ViewController2Delegate;
#interface ViewController2 : UIViewController
#property (nonatomic, weak) id<ViewController2Delegate> delegate;
#end
#protocol ViewController2Delegate <NSObject>
- (void)showSomethingByDelegate;
#end
viewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)buttonPressed:(id)sender {
[self.delegate showSomethingByDelegate];
}
#end
viewController.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController ()
<ViewController2Delegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.delegate = self;
}
// Below method was not called by delegate.
- (void)showSomethingByDelegate {
NSLog(#"Button Was Pressed!!!");
}
#end
well let me show an example more simple.
File: firstVC.h
/* This define the protocol object,
you can write methods required or optional the diference is when
the protocol is used in a class, xcode show you a yellow warning with required methods
like UITableViewDelegate, UITextFieldDelegate... */
#protocol firstVCDelegate <NSObject>
#required
- (void)didMessageFromOtherViewController: (NSString *)messageStr;
#optional
- (void)didOtherMessageNotRequired: (NSString *)messageStr;
#end
/* This is the definition of first UIViewController */
#interface firstViewController : UIViewController
#end
/* This is the definition of second UIViewController object with
a property that is our protocol 'firstVCDelegate' */
#interface secondViewController : UIViewController
#property (nonatomic, weak) id <firstVCDelegate> firstVCDelegate;
- (void)executeDelegateProcess;
#end
File: firstVC.m
#import "firstVC.h"
#pragma mark - First UIViewController with delegate
#interface firstViewController() <firstVCDelegate>
#end
#implementation firstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Creating the 'secondViewController' object with delegate on this class
secondViewController *svc = [secondViewController new];
// Assign the delegate class
[svc setFirstVCDelegate: self];
// Run the delegate logic
[svc executeDelegateProcess];
}
- (void)didMessageFromOtherViewController:(NSString *)messageStr
{
// Receiving the message from the instance of our protocol in the 'secondViewController' class
NSLog(#"MESSAGE #1: %#", messageStr);
}
- (void)didOtherMessageNotRequired:(NSString *)messageStr
{
// Receiving the message in optional method
NSLog(#"MESSAGE #2: %#", messageStr);
}
#end
#pragma mark - Second UIViewController
#implementation secondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)setFirstVCDelegate:(id<firstVCDelegate>)firstVCDelegate
{
if (firstVCDelegate)
_firstVCDelegate = firstVCDelegate;
}
- (void)executeDelegateProcess
{
// This method is only for demo
// You can execute your delegate in the way you need to use
if (_firstVCDelegate) {
[_firstVCDelegate didMessageFromOtherViewController: #"Hello world, using the required method from secondViewController class"];
[_firstVCDelegate didOtherMessageNotRequired: #"Hello code using the optional method"];
}
}
#end
In your appDelegate.m in the method didFinishLaunchingWithOptions you can put this, and you need to #import "firstVC.h"
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.window.autoresizesSubviews = YES;
[self.window makeKeyAndVisible];
[_window setRootViewController: [firstViewController new]];
Execute and see two logs messages, I hope I've helped :)
ViewController2 *vc2 = [[ViewController2 alloc] init];
This line of code is not doing what you think it is, this is creating a NEW instance of ViewController2 and storing it inside the variable vc2. At no point (based on your code) is this variable added to the navigation stack and displayed on screen.
Edit
Also as noticed by Holex, your vc2 variable will be removed from memory after the viewDidLoad is finished as you are not holding onto a reference of it. You would either need to create a property to hold onto it, or push it onto the navigation stack for the navigationController to hold onto it.
There are multiple ways to solve this issue:
Instead of creating a new viewController2, find a reference to the one already displayed on the screen (if it is on the screen).
If its not on the navigation stack, add vc2 to the navigation stack [self.navigationController pushViewController:vc2 animated:YES]; (assuming you have a navigation controller).
If not on the stack, and not intending to use a navigationController, present vc2 modally, such as [self presentViewController:vc2 animated:YES completion:nil];
I tried many custom delegate examples in my code to send data from view B back to parent view A. Why am I getting NULL? There should be something I am missing. Please help. How can I call setupDate to receive (NSDate *)setAlarmDate in Parent view?
Parent view
#import "SetupViewController.h"
#interface ViewController : UIViewController <SetupViewControllerDelegate> {
NSTimer *timer;
}
#end
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"setupview"]) {
NSLog(#"prepare for segue");
SetupViewController *svc = [[SetupViewController alloc] init];
svc.delegate = self;
-(void)setupDate:(NSDate *)setAlarmDate{
NSLog(#"Hey Hey");
NSLog(#"%#", setAlarmDate);
}
#end
View B
#protocol SetupViewControllerDelegate <NSObject>
#required
-(void)setupDate:(NSDate *)setAlarmDate;
#end
#interface SetupViewController : UIViewController
#property (nonatomic, strong) id <SetupViewControllerDelegate> delegate;
//IB
#property (weak, nonatomic) IBOutlet UIDatePicker *pDatePicker;
#end
.m
#import "SetupViewController.h"
#import "ViewController.h"
#implementation SetupViewController
-(void)viewWillDisappear:(BOOL)animated{
NSDate *date = [self.pDatePicker date];
[[self delegate] setupDate:date];
}
#end
You should access the UIViewController using the segue's destinationViewController property instead of allocating an instance of your own, which is why your alarmDate = NULL.
E.g.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"setupview"])
{
NSLog(#"prepare for segue");
SetupViewController *svc = [segue destinationViewController];
svc.delegate = self;
}
}
Also, as #Neru mentioned your delegate should be qualified as weak and not strong. The reason for this is because you will cause a retain cycle if you do not explicitly set the delegate = nil in the dealloc method.
I have three navigation buttons in a view controller, two of which I process locally and one which launches an edit view controller. I have created a segue from the body of the first UITableView to the Navigation Controller of the destination view controller (the edit view controller).
The button works fine (i.e. launches the edit view) but it doesn't receive the string I send to it. Could someone please help. I have tried all of the suggestions given to other related questions but cannot seem to fix this. Following is the related code.
Source controller.h:
#import <UIKit/UIKit.h>
#import "EQHorseAddViewController.h"
#import "EQDatabase.h"
#interface EQHorseDetailViewController : UITableViewController
#property (strong, nonatomic) EQHorseAddViewController *addViewController;
Source controller.m:
#import "EQHorseDetailViewController.h"
#interface EQHorseDetailViewController ()
#end
#implementation EQHorseDetailViewController
#synthesize addViewController;
-(void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *deleteItem= [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteHorse:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self
action:#selector(editHorse:)];
NSArray *actionButtonItems = [[NSArray alloc] initWithObjects:deleteItem, editItem,
nil];
self.navigationItem.rightBarButtonItems = actionButtonItems;
}
-(void)editHorse:(id)sender
{
NSLog(#"EditingHorse");
self.addViewController= [[EQHorseAddViewController alloc] init];
addViewController.resultSegue = #"Edit Horse Details";
[self performSegueWithIdentifier:#"EditHorseDetails" sender: self];
}
Destination View Controller.h:
#import <UIKit/UIKit.h>
#import "EQHorseDetailsInfo.h"
#import "EQDatabase.h"
#class EQHorseAddViewController;
#protocol EQHorseAddViewControllerDelegate <NSObject>
- (void) eqHorseAddViewControllerDidCancel:(EQHorseAddViewController *)controller;
- (void)eqHorseAddViewControllerDidSave: (EQHorseAddViewController *)controller;
#end
#interface EQHorseAddViewController : UITableViewController
<EQHorseAddViewControllerDelegate>
#property (strong, nonatomic) EQHorseAddViewController *addViewController;
#property (nonatomic, weak) id <EQHorseAddViewControllerDelegate> delegate;
#property (strong,nonatomic) NSString *resultSegue;
Destination View Controller.m
#import "EQHorseAddViewController.h"
#import "EQDatabase.h"
#import "EQHorseDetailsInfo.h"
#interface EQHorseAddViewController ()
#end
#implementation EQHorseAddViewController
#synthesize resultSegue;
- (void)viewDidLoad
{
NSLog(#"resultSegue %#",resultSegue);
}
Thanking you in advance.
It looks like you're referring to the code in editHorse:, which allocates an EQHorseAddViewController and sets a property on it. The problem with that code is that the view controller it allocates bears no relation to the vc destination of the segue. That method simply creates a vc and then instantly discards it.
Remove the allocation and instead implement prepareForSegue:. The segue object passed to that method will have a property called destinationViewController. That's the view controller that's about to be pushed onto the navigation. That's the one on which you should set the resultSegue property.
-(void)editHorse:(id)sender
{
NSLog(#"EditingHorse");
[self performSegueWithIdentifier:#"EditHorseDetails" sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"EditHorseDetails"]) {
EQHorseAddViewController *vc = segue.destinationViewController;
vc.resultSegue = #"Edit Horse Details";
}
}
You need to define the new controller as follows:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EQHorseAddViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"EditHorseDetails"];
ivc.resultSegue = #"Edit Horse Details";
Perhaps you'll need this method too
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"EditHorseDetails"]) {
// Get destination view
EQHorseAddViewController *vc = [segue destinationViewController];
vc.resultSegue = #"Edit Horse Details";
}
}
-(void)editHorse:(id)sender
{
NSLog(#"EditingHorse");
resultSegue = #"Edit Horse Details";
[self performSegueWithIdentifier:#"EditHorseDetails" sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *navigationController = segue.destinationViewController;
EQHorseAddViewController *addViewController = [navigationController viewControllers]
[0];
// Get destination view
NSLog(#"ResultSegue in Prepare - %#",resultSegue);
addViewController.resultSegue = #"Edit Horse Details";
}
In a view, let's call it firstView I created a secondView as follows and pushed it if certain thing happened in the firstView:
SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
Now when I'm in the secondView if let say a button is pressed I want to go back to firstView and also pass back a value from secondView to the firstView (let say an integer value of a textfield from secondView to the firstView).
Here is what I tried:
#protocol SecondViewControllerDelegate;
#import <UIKit/UIKit.h>
#import "firstViewController.h"
#interface SecondViewController : UIViewController <UITextFieldDelegate>
{
UITextField *xInput;
id <SecondViewControllerDelegate> delegate;
}
- (IBAction)useXPressed:(UIButton *)sender;
#property (assign) id <SecondViewControllerDelegate> delegate;
#property (retain) IBOutlet UITextField *xInput;
#end
#protocol SecondViewControllerDelegate
- (void)secondViewController:(SecondViewController *)sender xValue:(int)value;
#end
And in the m file
- (IBAction)useXPressed:(UIButton *)sender
{
[self.delegate secondViewController:self xValue:1234]; // 1234 is just for test
}
And then in the firstView I did:
#import "SecondViewController.h"
#interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {
}
#end
And implemented:
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[self.navigationController popViewControllerAnimated:YES];
}
Now, the problem is for one in FirstViewController I get the warning that "No definition of protocol "SecondViewControllerDelegate" is found, and for two the delegate method (last piece of code above) does not get invoked at all. Can somebody please tell me what's wrong?
After this line
SecondViewController *secondVC = [[secondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
Add
secondVC.delegate = self;
Also instead of
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[self.navigationController popViewControllerAnimated:YES];
}
You should use
- (void) secondViewController:(SecondViewController *)sender xValue:(int)value
{
[sender popViewControllerAnimated:YES];
}
In FirstViewController .h file :
#import "SecondViewController.h"
#interface FirstViewController : UITableViewController <SecondViewControllerDelegate> {
SecondViewController *secondViewController;
}
#end
In implementation file , where you init SecondViewController instance next line assign self to delegate property :
secondViewController.delegate = self;
Next define delegate method :
- (void)secondViewController:(SecondViewController *)sender xValue:(int)value
{
NSLog ("This is a Second View Controller with value %i",value)
}
For problem 1: The #protocol definition for SecondViewControllerDelegate looks like it's in secondViewController.h; are you sure this file is imported in firstViewController.h? Otherwise it won't know about the protocol.
Problem 2: It might be totally unrelated to problem 1. Are you sure the action is hooked up properly? Can you put a NSLog() call in your useXPressed: to make sure that method is actually getting called when you expect it to?