I am trying to send simple data from viewControllerA to viewControllerB using delegate but can not find The problem in my code.here is the code
in ViewControllerA.h
#import <UIKit/UIKit.h>
#class ViewControllerA;
#protocol viewControllerADelegate <NSObject>
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;
#end
#interface ViewControllerA : UIViewController
- (IBAction)buttonPressed:(id)sender;
#property (assign) id<viewControllerADelegate> delegate;
#end
inViewControllerA.m
#import "ViewControllerA.h"
#interface ViewControllerA ()
#end
#implementation ViewControllerA
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonPressed:(id)sender {
[self.delegate addItem:self data:#"this is data"];
}
#end
and here is ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
#interface ViewControllerB : UIViewController<viewControllerADelegate>
#end
and ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
#interface ViewControllerB ()
#end
#implementation ViewControllerB
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba setDelegate:self];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item{
NSLog(#"delegate function called");
}
#end
The implemented function -(void) addItem: (ViewControllerA *)controller data:(NSString *)item is never called.Am I missing something ? Thanks in advance
Are you using storyboards? If yes, you just push the wrong button: storyboard initializes view controllers on its own, so yours code:
ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba setDelegate:self];
just setting delegate on some unused view controller.
Use segues, don't try to reinvent a wheel.
Related
I am using pop over button to Signout a page back to Root view controller. i successfully came back to root view controller but pop up not dismissing. It stays on the Root view controller Here is my code
POPOVERVIEW CONTROLLER.H
#protocol MJSecondPopupDelegate;
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
#import "TimeTableViewController.h"
#class TimeTableViewController;
#interface PopOverViewController : UIViewController <UIPopoverControllerDelegate>
{
// UIPopoverController *popoverController;
}
#property (assign, nonatomic) id <MJSecondPopupDelegate>delegate;//ede ga
#property(strong,nonatomic) UIPopoverController *popoverController;
#property(nonatomic,strong)TimeTableViewController *TimeObj;
-(IBAction)logOut:(UIButton *)sender;
#end
#protocol MJSecondPopupDelegate<NSObject>
#optional
- (void)cancelButtonClicked:(PopOverViewController*)secondDetailViewController;
#end
POPOVERVIEW CONTROLLER.M
#import "PopOverViewController.h"
#interface PopOverViewController ()
#end
#implementation PopOverViewController
#synthesize delegate,TimeObj,popoverController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)SignOut{
NSLog(#"protocol");
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"delegate value is %#",self.delegate);
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)logOut:(UIButton *)sender{
if (self.delegate && [self.delegate respondsToSelector:#selector(cancelButtonClicked:)])
{
[self.delegate cancelButtonClicked:self];
[self.popoverController dismissPopoverAnimated:YES];
}
}
MAINVIEW CONTROLLER.H
#import <UIKit/UIKit.h>
#import "NotesandReminders.h"
#import "Cell.h"
#import "SWRevealViewController.h"
#import "AvailableSessionViewController.h"
#import "ServiceConnector.h"
#import "AppDelegate.h"
#import "PopOverViewController.h"
#import "UIViewController+MJPopupViewController.h"
#class NotesandReminders;
#class PopOverViewController;
#protocol Popupprotocol <NSObject>
#optional
-(void)SignOut;
#end
#interface TimeTableViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate,MJSecondPopupDelegate>{
}
MAINVIEWCONTROLLER.M
#import "AvailableSessionViewController.h"
#interface TimeTableViewController ()
#end
#implementation TimeTableViewController
#synthesize pc,myCounterLabel,cancelSessionButtonOutlet,userButtonOutlet,saveButtonOutlet,timerr,collectionData,cancelSession,jsonData,secondviewcontroller,popoverController;
#synthesize startWeek,endWeek,startDateLabel,endDateLabel,todaysDate,startDate,endDate,dateForMatching,startDateToCall,endDateToCall;
#synthesize blueBarOutlet;
BOOL isLongPressed ;
int hours, minutes, seconds;
int secondsLeft;
int valueForBlueBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
// This method runs first when the screen is displayed
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"POPSegue"]) {
PopOverViewController* userViewController = [segue destinationViewController];
userViewController.delegate=self;
}
}
I am creating Pop over just by cntrl drag to the view controller and selecting PopOver modally
After the following line:
[self.popoverController dismissPopoverAnimated:YES];
add
[self.popoverController.contentViewController.navigationController
popToRootViewControllerAnimated:YES];
This is how i managed to get rid of it
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"POPSegue"]) {
// settings popover is the global variable for --> UIPopoverController *settingsPopOver;
settingsPopOver = [(UIStoryboardPopoverSegue *)segue popoverController];
_secondviewcontroller = (PopOverViewController *)settingsPopOver.contentViewController;
_secondviewcontroller.delegate = self;
}
}
I am super new to programming in general. I am trying to make a very simple app.
I am trying to pass data from one viewcontroller to another using a storyboard. On my first screen I have a text field and a button then after you press the button i want the second screen's label to be updated with whatever text you put in the text field.
Here is some of my code:
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize secondviewData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)passData:(UIButton *)sender {
// SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.secondviewData = secondviewData;
secondviewData.passedValue = input.text;
// [self presentViewController:second animated:YES completion:nil];
homeLabel.text = input.text;
}
#end
#import "SecondViewController.h"
#import "FirstViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize passedValue;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
label.text = passedValue;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Here's a link to my code: http://oberober.com/passingData/
So far the label on the second screen keeps going blank no matter what I enter on the first screen's text field.
I feel like I am missing an important concept? Any tips or tutorials I can look at?
Thanks ahead of time,
Casper
You pass the data by using the prepareForSegue method of the first controller. Here's an example from one of my apps that shows a vehicle object being passed to the second controller.
CHRBodyViewController is the class of the second controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CHRBodyViewController *body = [segue destinationViewController];
body.vehicle = _vehicle;
body.updated = YES;
}
"property implementation must have its declaration in interface" error
I have this error coming up and I am new to xcode so I am unsure what to do.
I had lots of code in a class, then i moved it to a different class and now I have numerous errors. Which dont make sense, as everything looks as it should.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize MenuScreenButton;
#synthesize HowToPlayButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)HowToPlayButtonPressed:(id)sender
{
HowToPlayViewController *HowToPlayView=[[HowToPlayViewController alloc]initWithNibName:nil bundle:nil];
HowToPlayView.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:HowToPlayView animated:YES completion:NULL];
}
-(IBAction)MenuScreenButtonPressed:(id)sender
{
MenuScreen *MenuScreenView=[[MenuScreen alloc]initWithNibName:nil bundle:nil];
MenuScreenView.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:MenuScreenView animated:YES completion:NULL];
}
#end
Below is the .h file:
#import <UIKit/UIKit.h>
#import "HowToPlayViewController.h"
#import "MenuScreen.h"
#interface ViewController : UIViewController
{
IBOutlet UIButton *HowToPlayButton;
IBOutlet UIButton *MenuScreenButton;
}
#property(retain,nonatomic) IBOutlet UIButton *MenuScreenButton;
#property(retain,nonatomic) IBOutlet UIButton *HowToPlayButton;
-(IBAction)HowToPlayButtonPressed:(id)sender;
-(IBAction)MenuScreenButtonPressed:(id)sender;
#end
I know this is a very repeat topic, but I can't get it works.
MainTab.h:
#import <UIKit/UIKit.h>
#interface MainTab : UITabBarController<UITabBarControllerDelegate, UITabBarDelegate> {
IBOutlet UITabBarController *tabController;
}
#property (nonatomic,retain) IBOutlet UITabBarController *tabController;
#end
MainTab.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(#"main tab");
[super viewDidLoad];
self.tabBarController.delegate = (id)self;
[self setDelegate:self];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"selected %d",tabBarController.selectedIndex);
}
I can't find what I'm missing, any help will be appreciated.
Now I try to link it into MainStoryBoard:
But it doesnt work, what are the connection?
On the basis of your #interface (and your subsequent screen snapshot), MainTab is the UITabBarController, so the following line:
self.tabBarController.delegate = (id)self;
Should just be:
self.delegate = self;
You don't want a tabBarController property in the UITabBarController, itself, nor do you want to use the self.tabBarController syntax. You only use that syntax if you're trying to reference the tab bar controller from one of its children controllers. When in the tab bar controller itself, just refer to self.
Thus, it work if MainBar is defined as:
// MainBar.h
#import <UIKit/UIKit.h>
#interface MainBar : UITabBarController
#end
And
// MainBar.m
#import "MainBar.h"
#interface MainBar () <UITabBarControllerDelegate>
#end
#implementation MainBar
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"selected %d",tabBarController.selectedIndex);
}
#end
And don't forget to set the class of the tab bar controller:
The connections inspector (where I didn't touch a thing) looks like:
I try to implement User define Delegate.For that i did this code
**- DelegateAppDelegate.h**
#import <UIKit/UIKit.h>
#import "viewApp.h"
#interface DelegateAppDelegate : NSObject <UIApplicationDelegate,SampleDeledate>
{
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
**- DelegateAppDelegate.h**
#import "DelegateAppDelegate.h"
#implementation DelegateAppDelegate
#synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewApp *v=[[view alloc]initWithNibName:#"viewApp" bundle:nil];
[_window addSubview:v.view];
return YES;
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
#end
#implementation DelegateAppDelegate(SampleDeledate)
-(void)AppImageDidLoad:(NSString*)Name;
{
NSLog(#"hi........%#",Name);
}
#end
**- viewApp.h**
#import <UIKit/UIKit.h>
#class viewApp;
#protocol SampleDeledate;
#protocol SampleDeledate <NSObject>
#required
-(void)AppImageDidLoad:(NSString*)Name;
#end
#interface viewApp : UIViewController
{
id<SampleDeledate>delegate;
}
#property(nonatomic,assign)id<SampleDeledate>delegate;
-(IBAction)DelegateFunction:(id)sender;
#end
**- viewApp.m**
#import "viewApp.h"
#implementation view
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) { }
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(IBAction)DelegateFunction:(id)sender
{
[self.delegate AppImageDidLoad:#"Delegate sample"];
}
#end
In this coding my intention is When i click button, Corresponding Action get executed( -(IBAction)DelegateFunction:(id)sender),then i want to call the AppImageDidLoad delegate method, But in my coding this is not working,
1)Why this is not working,Any wrong i did?
thanks for your replay
In the following method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewApp *v=[[view alloc]initWithNibName:#"viewApp" bundle:nil];
[_window addSubview:v.view];
return YES;
}
add one more line and make it as follows.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
viewApp *v=[[view alloc]initWithNibName:#"viewApp" bundle:nil];
v.delegate = self;
[_window addSubview:v.view];
return YES;
}