I have a method in my ViewController that opens different views based on a pin being present, it works when assigned to a button on an intial view, I can also put it in viewDidLoad method however I am using storyboard and this method viewDidLoad is firing on each view load from modal segues, so instead I am looking to call the method from my appDelegate didFinishLaunchingWithOptions.
It is basically a view redirect on app launch based on the presence of a stored pin value.
I have imported the view controller in app delegate #import "ViewController.h"
then using the current code in didFinishLaunchingWithOptions:
ViewController *rootViewController = (ViewController*)self.window.rootViewController;
[rootViewController initView];
I do this but I am getting the error
No Visible interface for 'ViewController' declares the selector 'initView'?
Here is my full ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UITextField *emailField;
IBOutlet UITextField *passwordField;
IBOutlet UITextField *pinField;
IBOutlet UITextField *checkPin;
}
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#property (strong, nonatomic) IBOutlet UIBarButtonItem *ShowEmailList;
-(IBAction)showList:(id)sender;
-(IBAction)checkPin:(id)sender;
-(IBAction)initView:(id)sender;
#end
and in my ViewController.m file the method:
-(IBAction)initView:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *getPin = [defaults objectForKey:#"Pin"];
if ([getPin isEqualToString:#""]){
//do something
}else{
//do something else
}
}
Any ideas on how to correctly declare my initView method in my ViewController.h file to correct this error?
Thanks
You need add an argument to this method like:
[rootViewController initView:nil];
Your -(IBAction)initView:(id)sender is expecting a parameter.
You have two ways to fix this.
Modify your method signature as -(IBAction)initView { //source code. }
Pass nil or the AppDelegate from the caller method
[rootViewController initView:nil]; or [rootViewController initView:self];
Related
I have a sequencer with each track that has buttons as an outlet collection. The code all works fine in it's own view controller however I want to transfer all the methods to a singleton so that I can control the playback from other views.
for instance I have
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *trackOneOutletCollection;
However I have methods which act on the alpha and tags of each button; the methods contain these vales which I don't know how to access from the singleton. I thought the singleton was where I store all the data and then call it from the class file view controller?
You can use Inheritance concept to achieve this functionality.you need to create one ParentViewController that hold IBOutletCollection property. and rest of all View Controller is child of ParentViewController. then you can access IBOutletCollection in other view ontroller. like this way.
ParentViewController:-
#interface ParentViewController : UIViewController
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *trackOneOutletCollection;
#end
ChildViewController;-
#interface YourViewController : ParentViewController
#end
.m file
#implementation YourViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"trackOneOutletCollection = %#"self.trackOneOutletCollection);
}
#end
I have the delegate method I implement to pass data from FirstViewController to SecondViewController.
First, I created a text Field and Button in FirstViewController. Second, I created a label to display the data in SecondViewController.
How it should work:
The user will press the button in FirstViewController and it will pass the data to the secondViewController.In addition,the user will be using a UISwipeGestureRecognizer to navigate to the second view controller, so there's no need to use the button to go to the second view controller.
How I test the application
1- I run the application. --> 2- I type "Hello" in Text field --> 3- I press the button --> 4- I swipe to the second view controller to see the data --> 5- I don't see any data in the label ??
First i created the delegate protocol
FirstViewController.h
#class FirstViewController;
#protocol FirstControllerDelegate
-(void) updateLabel:(NSString*)string
#end
#property (weak, nonatomic) id<FirstViewControllerDelegate>delegate;
#property (weak, nonatomic) IBOutlet UITextField *TextField;
- (IBAction)button:(id)sender;
FirstViewController.m
#interface FirstViewController. ()
#end
#implementation ViewController1
#synthesize delegate;
#synthesize TextField;
-(void)viewDidLoad{
[super viewDidLoad];
}
- (IBAction)Button:(id)sender {
ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.stringfromTextfield1 = self.TextField.text;
}
SecondViewController.h
#import "SecondViewController.h"
#import "FirstViewController.h"
#interface SecondViewController : UITableViewController<FirstControllerDelegate>
#end
#property (weak, nonatomic) IBOutlet UILabel *Label;
#property (strong, nonatomic) NSString *stringfromTextfield1;
SecondViewController.m
#interface SecondViewController. ()
#end
#implementation SecondViewController
-(void)viewDidLoad{
[super viewDidLoad];
self.label.text = self.stringfromTextfield1;
}
I appreciate your time and effort to help me out
You can use Singleton class for this, or you can use extern NSString for move the string value one to second View Controller.
in first view controller.
extern NSString *MyStr;
in the #interface define it.
#interface{
NSString *MyStr;
}
In the #Implementation assign the value what you want.
and the second view controller just #import the first view and use,
MyStr As a variable and its having the value also from first view controller.
This thing worked for me.
Or simply add an AppDelegate property to store the variable in both ViewControllers:
AppDelegate *appDel=[[UIApplication sharedApplication] delegate];
appDel.variable=#"text";
You can save data in App delegate and access it across view controllers in your application.You have to create a shared instance of app delegate.
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
like this
Suppose you declare a NSString object *strthen you can access it in any view controller by appDelegate.str
I'm new to iOS programming, and I'm not sure why my textDidChange function is not firing. Searched online a bunch, but can't find out what's different between my code and everyone else's. Here's what my .h and .m files look like for this view controller:
CategoryTableViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface CategoryTableViewController : UITableViewController
#property (weak, nonatomic) IBOutlet UIBarButtonItem *btnBack;
#property (weak, nonatomic) IBOutlet UISearchBar *txtSearchBar;
#property (strong,nonatomic) NSArray *allCategories;
#property (strong,nonatomic) NSMutableArray *filteredCategories;
//A stack containing the parent categories used to get to the current category.
#property (strong, nonatomic) NSMutableArray *parentCategories;
#end
Relevant code from CategoryTableViewController.m:
-(void)txtSearchBar:(UISearchBar*)txtSearchBar textDidChange: (NSString*)text
{
//do something
}
I used Xcode's ctrl+click+drag to create the reference to the search bar in the header file. I put breakpoints and print statements at the start of the textDidChange code, but none of it ever gets called. Any ideas?
You need to set the delegate property of txtSearchBar to use the delegate methods.
Make sure you add
self.txtSearchBar.delegate = self;
in ViewDidLoad()
and the delegate method
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//Do something
}
If you're using a delegate method be sure to set the delegate property on the instance that needs to call the delegate so it knows there is a delegate and where to send actions.
if you're using Interface Builder and not delegate, you need to create an IBAction function, or you can use a UIControl's subclass addTarget() method to create a callback for a given event type.
Add
self.txtSearchBar.delegate = self
in viewDidLoad().
I have an initial ViewController, lets call it HomeViewController, that has a button which calls a modal view controller, lets call it ModalViewController. Inside that ModalViewController I have a table view with two sections. If you click on any cell from section 0 it sends information back to HomeViewController (this part I have working with Protocol). If you click on any of the cells from section 1 it pushes to another view controller with options, lets call it OptionsViewController. Here is where it gets tricky for me. If you click any of those options, dismiss OptionsViewController and close ModalViewcontroller and send that information to HomeViewController, just like ModalViewController to HomeViewController. I have tried to set up a protocol similar to the one in ModalViewController but it is never called.
OptionsViewController protocol & .h file
#protocol OptionsViewControllerDelegate <NSObject>
#optional
-(void) optionsInfo:(NSArray *)optionsViewArray;
#end
#interface OptionsViewController : UITableViewController
#property (retain) id delegate;
#property (nonatomic, strong) NSArray *sendArray;
#end
OptionsViewController.m where it's called to pop off the stack.
{
[self dismissOptionsView];
}
-(void) viewWillDisappear:(BOOL)animated
{
NSLog(#"Send Array: %#", self.sendArray);
[[self delegate] optionsInfo:sendArray];
}
-(void)dismissOptionsView
{
[self.navigationController popViewControllerAnimated:YES];
}
Inside ModalViewController.h
#protocol ModalViewControllerDelegate <NSObject>
#optional
-(void) sendInformation:(NSArray *)infoArray;
#end
#interface ModalViewController : UITableViewController <ConditionsViewControllerDelegate, UISearchBarDelegate>
{
UISearchBar *searchDrugBar;
}
#property (retain) id delegate;
#property (nonatomic, strong) IBOutlet UISearchBar *searchDrugBar;
#property (nonatomic, strong) NSArray *infoArray;
#end
ModalViewController.m where OptionsInfo is supposed to come in.
-(void) optionsInfo:(NSArray *)optionsViewArray
{
//IT NEVER REACHES HERE :(
NSLog(#"HERE");
self.infoArray = optionsViewArray;
NSLog(#"Finished");
[self dismissViewControllerAnimated:YES completion:nil];
}
Has any one has done something similar like this or knows the solution to this? Any information, link, guidance and etc. to the right direction will be appreciated. Thanks in advance.
You need to set the delegate in your OptionsViewController below:-
In your OptionsViewController.m Include below line on your method
[self setDelegate:ModalViewController];
I am trying to equal the string of an UITextView from anotherViewController to the MainViewController . I mean users write something in UITextView from anotherViewController and when touch the done button the string in UITextView should equal to another string in MainViewController . How can I access UITextView ???
I did something like this but did not get any result !
#import "AnotherViewController"
#class AnotherViewController;
#interface MainViewContoller : UIViewController {
NSString *main_string;
AnotherViewController *textEntered;
}
-(void)viewDidLoad {
textEntered.TEXT = main_string
}
***TEXT is the name my UITextView on AnotherViewController .
EDITED :
#interface AnotherViewController : UIViewController {
UITextView *TEXT;
id delegate;
}
#property (nonatomic, retain) IBOutlet UITextView *TEXT;
#property (nonatomic ,retain) id delegate;
#end
#implementation AnotherViewController
#synthesize TEXT ,delegate;
- (IBAction)doneButton {
//!!! problem with compare ! the compiler got me some warning
[self dismissModalViewControllerAnimated:YES];
}
MAIN VIEW CONTROLLER
#import "AnotherViewController.h"
#class Another...;
#interface MainViewControlelr : UIViewController {
NSString *main_string;
TextViewController *textEntered;
}
- (void)viewDidLoad
{
textEntered.delegate = self;
}
- (void) compareText {
[textEntered.TEXT isEqual:main_string];
}
Set mainViewController object as the delegate of an object of anotherViewController type. And use that delegate from AnotherViewController to pass messages to MainViewController.
Inside MainViewController, when you create your AnotherViewController object do:
anotherVC.delegate = self;
And in AnotherViewController, when your button action is done and you want to pass the text back, say store in a string. Then pass it to MainViewController as:
[delegate compareText:textView.text];
compareText with a NSString argument would be a method in your MainViewController class.
EDIT:
In your AnotherViewController.h interface, declare an ivar:
id delegate;
Then use #property (in .h) and #synthesize (in .m) for getter and setter.
Then do the above.
I think you created an AnotherViewController pointer but did not set it.
before you can access textEntered.TEXT, you should set you textEntered pointer to your AnotherViewController object.
Here is how:
on iGhalamViewController class, create a property for setting textEntered object like this in your interface declaration (probably in iGhalamViewController.h)
#property (assign) AnotherViewController *textEntered;
So, at the root place where do you create this viewcontroller objects both. After you create AnotherViewController and iGhalamViewController, set your textEntered pointer like this.
AnotherViewController* a = [[AnotherViewController alloc] initWithBlah:blah];
iGhalamViewController* b = [[iGhalamViewController alloc] initWithBlah:blah];
b.textEntered = a;
Then it should work like you expected. But use it in other method than viewDidLoad. viewDidLoad probaby gets called before you can set textEntered. A button press event method should be fine.