User Define Delegate Not working? - objective-c

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

Related

Delegate not informing my View

My cpViewController has a button that modal segues to cpScanViewController. I want to have cpScanViewController inform ViewController with a string when it has completed a successful scan. After reading numerous articles online, I believe delegation is the best way to do this?
Is the delegate being set correctly?
How does the delegate inform the cpViewController?
cpScanViewController.h
#import <UIKit/UIKit.h>
#protocol ScanDelegate <NSObject>
-(void)receivedUPC:(NSString *)Scan;
#end
#interface cpScanViewController : UIViewController
#property (nonatomic, weak) id <ScanDelegate> delegate;
#property (nonatomic, retain) NSString *UPC;
-(void)checkUPC;
#end
cpScanViewController.m
#interface cpScanViewController ()
{
NSString *UPC;
}
#end
#implementation cpScanViewController
#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)doSomeScanStuff
{
UPC = #"a string"
// even tried
[delegate receivedUPC:UPC];
}
-(void)checkUPC
{
}
#end
cpViewController.h
#import <UIKit/UIKit.h>
#import "cpScanViewController.h"
#interface cpViewController : UIViewController <ScanDelegate>
#end
cpViewController.m
#interface cpViewController ()
{
cpScanViewController *cp;
}
#end
#implementation cpViewController
- (void)viewDidLoad
{
// set delegate
cp = [[cpScanViewController alloc] init];
[cp setDelegate:self];
}
-(void)receivedUPC:(NSString *)Scan{
// Nothing happening
NSLog(#"%#", Scan);
NSLog(#"The %#", cp.UPC);
}
#end
Because you are not calling receivedUPC method from your cpScanViewController class. You need to add [_delegate receivedUPC:self], some where in order to invoke this delegat method in cpViewController class.
I was able to get it working by using the method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
cpScanViewController *vd = (cpScanViewController *)[segue destinationViewController];
vd.delegate = self;
}
Because I made the connection in storyboards I didn't realize it needed to have the method.

Pop up not Dismissing when i click button in logout

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

property implementation must have its declaration in interface error

"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

Passing data from controllerA to controllerB using delegate

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.

Objective C - Incompatible pointer types assigning to 'int*'

In AppDelegate.m Why I got this message error
Incompatible pointer types assigning to 'int*' from 'MyPlugin*'
on this line:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myPlugin = [[MyPlugin alloc] init];
In AppDelegate.h I have:
#property (strong, nonatomic) MyPlugin *myPlugin;
In MyPlugin I have:
#import "MyPlugin.h"
#import <UIKit/UIKit.h>
#implementation MyPlugin
- (void)dealloc {
[super dealloc];
}
- (MyPlugin*)init
{
if (self = [super init]) {
}
return self;
}