Pop up not Dismissing when i click button in logout - objective-c

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

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.

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

TabBarController didSelectViewController not working

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:

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.

User Define Delegate Not working?

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