Crash when set value to parent controller in Singleton - ios

AnspruchstellerVC and VersicherungsnehmerVC inherited from BaseVC.
#interface BaseVC : UIViewController
#property (nonatomic, strong) NSNumber *tag;
#end
Can set value for AnspruchstellerVC but for VersicherungsnehmerVC it isn't work.
- (UIViewController *)loadViewControllerByID:(NSString *)vcID
{
NSParameterAssert(vcID);
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:vcID];
return vc;
}

Everithing looks just fine.
Try to clean project and build folder.
good luck ;)

Related

How to Add Login to my iOS PDF reader App?

So I'm trying to add a login to my existing app. My App has two Storyboards one for iPhone and another for iPad. I added a login view set to rootViewController but I can't seem to load my UITabBarController correctly.Normally a PDF should load. I add the login that then segs to the UITabBarController but nothing is loaded its just an empty storyboard. I'm not loading it(the tab bar and document) properly or something. I'm a noob any and all help appreciated.
App Delegate file:
#interface AppDelegate () <UITabBarControllerDelegate>
#property (nonatomic, strong) PDFDocumentStore *documentStore;
#property (nonatomic, strong) DocumentListViewController *documentsViewController;
#property (nonatomic, strong) DocumentListViewController *recentViewController;
#property (nonatomic, assign) BOOL launchingWithURL;
#property (nonatomic, assign) BOOL addSkipBackupAttributeToItemAtURL;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self migrateIfNeeded];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:#"Login"];
[[UIApplication sharedApplication].keyWindow setRootViewController:rootViewController];
self.documentStore = PDFDocumentStore.new;
[self.documentStore.rootFolder load];
UITabBarController *tabBar = (UITabBarController *)[[self window] rootViewController];
tabBar.delegate = self;
self.documentsViewController = (DocumentListViewController *)[tabBar.viewControllers[0] topViewController];
FolderDocumentListViewModel *folderModel =
[[FolderDocumentListViewModel alloc] initWithFolder:self.documentStore.rootFolder];
self.documentsViewController.viewModel = folderModel;
[self.documentsViewController view];
self.recentViewController = (DocumentListViewController *)[tabBar.viewControllers[1] topViewController];
RecentDocumentListViewModel *recentModel =
[[RecentDocumentListViewModel alloc] initWithDocumentList:self.documentStore.documentList];
self.recentViewController.viewModel = recentModel;
NSString *pdf = #"2016 Product Handbook";
NSString *toPath = [[[NSFileManager grt_documentsPath]
stringByAppendingPathComponent:pdf]
stringByAppendingPathExtension:#"pdf"];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:toPath]];
NSURL *URL = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
if (URL) {
self.launchingWithURL = YES;
}
return YES;
}
Create a UITabBarController in your `AppDelegate.h' file.
#property (strong, nonatomic) UITabBarController *tabBarController;
-(void)userDidLoginSuccessfully;
And then declare a public function in AppDelegate.h file. (Lets say userDidLoginSuccessfully is my function in this case).
In Your AppDelegate.m file write this function.
-(void)userDidLoginSuccessfully{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
self.tabBarController = [[UITabBarController alloc]init]
self.tabBarController = [storyboard instantiateViewControllerWithIdentifier:#"MyTabBarController"];
[self.window setRootViewController:self.TabBarController];
}
Then in you LoginViewController where you check the success of login within if-else block just write these lines.
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate userDidLoginSuccessfully];
Do you want to load UITabBarController after success of login. Right?

How to pas data between view controller without using segue in ios?

Helo all,
I am moving from one view controller to another using below code.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Now i want to pass some data from one view controller to another like some variable.I have got this code to do this task but i am not making instance of another view controller.I am starting another view controller using storyboard.
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];
Please tell me how can i pass data between view controllers in my way?
Solution does not work
decalre NSString in .h file.
#property (nonatomic, strong) NSString *UserId;
Synthesize in .m file
#synthesize UserId;
Code for navigation
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.UserId=#"abc";
[self.navigationController pushViewController:vc animated:YES];
But vc.UserId does not recognized.
You can pass data between two ViewControler with the help of properties.
First in RoloBusinessCard.h ViewControler make a property like this
#property (nonatomic, strong) NSString *str1;
in .m file of this class synthesize it like this
#synthesize str2;
Now like this you can pass value
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.str2=str2
[self.navigationController pushViewController:vc animated:YES];
If you are not able to get str2 in RoloBusinessCard
First Replace UIViewController with Your Viewcontroller Name like this
RoloBusinessCard *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
or
typecase UIViewController like this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
(RoloBusinessCard *)vc1=vc
vc1.str2=str2
[self.navigationController pushViewController:vc1 animated:YES];
Hope it help.
Try Like this, Hope it will work.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RoloBusinessCard"];
vc.str1 = str;
[self.navigationController pushViewController:vc animated:YES];
Here we are just using Navigation stack for passing data to the next controller
create a property in 2nd view controller
#property (nonatomic, strong) NSString *yourString;
in firstView controller action method
object of view controller which you will create on push method do
-(void)btnAtion{
object.yourstring = txtyourfirstvcObject.text
}
In swift, we can do the same
let's suppose we want to pass a Bool Value to the next controller so we can do :
Class ToViewController : UIViewController {
public var isSelected : Bool?
}
Class FromViewController : UIViewController {
#IBAction func onClickFlyingFrom(_ sender: UIButton) {
let tvc = self.storyboard?.instantiateViewController(withIdentifier: "ToViewController") as! ToViewController
tvc.isSelected = true
self.navigationController?.present(vc, animated: true, completion: nil)
}
}

Declaring UIViewController variable property on another ViewController with Swift

I am a newbie to Swift. I have started a new project with Swift. I am trying to add SecondViewcontroller view as a subview to the FirstViewController. What i am looking is to declare the SecondViewController property for the FirstViewController. Can anyone please suggest the Swift version of the below code
FirstViewController.h
#interface FirstViewController : UIViewController {
IBOutlet UIView *listContainerView;
}
#property (nonatomic, strong) SecondViewController *secondVC;
#end
FirstViewController.m
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.secondVC = (SecondViewController *)[mainStoryBoard instantiateViewControllerWithIdentifier:#"SecondViewController"];
if (![self.secondVC.view isDescendantOfView:self.view]) {
[self addChildViewController:self.secondVC];
self.secondVC.view.frame = CGRectMake(0, 0, listContainerView.frame.size.width, listContainerView.frame.size.height);
[listContainerView addSubview:self.secondVC.view];
[self.secondVC didMoveToParentViewController:self];
}
}
#end
What you should do is to make a subview, instead of making a new UIViewController. In only very special cases would you ever put one UIViewController inside another. Basically every 'screen' should have one UIViewController, but you shouldn't put one inside another.

Set Label text equals to variable

I'm writing a program for fun, and in it I have a UIViewController with four buttons, they all send the user to another view, but depending on what button is pressed, I want the label in that other view to have a different text. To do that, I decided to have the label's text to be set equals to a NSString variable, and that variable is set to a certain text depending on what button is pressed. So I have this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
[self presentViewController:vc animated:YES completion:nil];
_myLabel.text = [NSString stringWithFormat:myText];
In which myText is the NSString variable I want the label to be set to.
Everything seems to be in order, but the label won't have it's text changed. If I try to change it's text inside the viewDidLoad function, though, it works just fine, so I don't know what is happening.
Any idea of what I might need? If you need more info, just comment what you want and I will try to provide it.
You cannot update UI of other controller from current one - you can set variables and later on in viewDidLoad or viewWillAppear you can assign those values to UI
In File: FirstViewController.m
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SecondViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
vc.passedText = #"Hello Next";
[self.navigationController pushViewController:vc animated:YES];
In File: SecondViewController.h
#interface SecondViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *myLabel;
#property (strong, nonatomic) NSString *passedText;
In File: SecondViewController.m
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.myLabel.text = self.passedText;
}
I think you need to set your ViewController's property before calling the:
[self presentViewController:vc animated:YES completion:nil];
If you set it after presenting the view, you have to manually refresh the view, which is a bit of an overkill. Just set it before presenting the view. So it would look like:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ResultScreenViewController *vc = (ResultScreenViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
vc.customTitleLabel.text = [NSString stringWithFormat:myText];
[self presentViewController:vc animated:YES completion:nil];
In your ViewController header file:
#property (nonatomic, strong) UILabel *customTitleLabel;
First Create a subclass of a UIViewController with a custom label:
#interface NextViewController : UIViewController
#property (nonatomic, strong) UILabel* customLabel
#end
Now back to your code. You simple cast the UIViewController you get to the subclass you created so you can access the label inside you view controller.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NextViewController* nextVC = (NextViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
nextVC.customLabel.text = [NSString stringWithFormat:myText];
[self presentViewController:nextVC animated:YES completion:nil];
Ok, with the help of one of the answers here I found I solution that works for me. Before, when I had
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
[self presentViewController:vc animated:YES completion:nil];
_myLabel.text = [NSString stringWithFormat:myText];
I didn't realise that the resultScreen wasn't linked to UIViewController, but to ViewContronller. So all I needed to do was change the vc declaration to
ViewController *vc = (ViewController *)[mainStoryboard instatiateViewControllerWithIdentifier:#"resultScreen:];
So then all I needed to do was change the label's text at the ViewDidLoad module. It works just fine for me now.
Its simple..
if ([_mylabel.text isEqualToString: myText)
{
//--- Now process your method here. Show alert or what else you need
}
try this. I think that accessing UIlabel directly of other UIViewController is not a good approach. Follow this & it will definitely work.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
resultScreen *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"resultScreen"];
vc.strText = myText;
[self presentViewController:vc animated:YES completion:nil];
In your resultScreen view controller create a NSString property.
resultScreen.h
#property (retain, nonatomic) NSString *strText;
resultScreen.m
-(void)viewDidLoad
{
self.myLabel.text = self.strText;
}
you are trying to set the labelText before presenting the second View.
Before view gets loaded into the memory, its all outlets are nil.
so use the NSString variable in second view, and try to set the value of that variable,and then in
viewWillAppear: method assign the value of string variable to the labelText, this will definitely work.
Cheers!

Call NavigationController (xib) from ViewController (storyboard)

I'm normally just use storyboard for my iOS, but needed to integrate an SDK which does not use storyboard, rather xib. My initial view controller is from storyboard, and when a button is pushed (IBAction), I would like it to go to the xib view controller, but not sure how programmatically to do so. Here is my AppDelegate:
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:...{
// set to storyboard on launch
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"iPhoneStoryboard"
bundle: nil];
UIViewController* viewController = [mainStoryboard instantiateInitialViewController];
[self.window setRootViewController:viewController];
[window makeKeyAndVisible];
return YES;
}
Here is my code .h:
//mainVC.h
#import <UIKit/UIKit.h>
#interface MainVC : UIViewController{
UIWindow *window;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIButton *buttonScan;
-(IBAction) ActionScan;
#end
And the .m:
//mainVC.m
#interface MainVC ()
#end
#implementation MainVC
#synthesize window;
#synthesize navigationController;
#synthesize buttonScan;
- (IBAction)ActionScan{
window.rootViewController = navigationController;
[window makeKeyAndVisible];
}
create instance of your xib viewController with initWithNibName
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
& then push this controller instance on navigation controller
[self.navigationController pushViewController:desController animated:YES]
In the view controller in storyboard, code below:
alloc & init destination view controller:
UIViewController *desController = [[SomeViewController alloc]init];
show the destination view controller:
[self.navigationController pushViewController:desController animated:YES]
or
[self.navigationController presentViewController:desController animated:YES completion:nil
Usually you don't set the window's rootViewController directly. Instead you should use
- (IBAction)ActionScan{
[self presentViewController:self.navigationController animated:YES completion:nil];
}
to show your next view controller. The first and accepted answer to this question gives a more detailed explanation of how this works.
Changing root view controller of a iOS Window
Setting window.rootViewController and calling [window makeKeyAndVisible] usually happens in your app delegate, but since you're using storyboards, it is done for you under the hood.
Also, unless you're using multiple windows, you can access your UIWindow from anywhere using your app delegate singleton object.
MyAppDelegateClass *appDelegate = (MyAppDelegateClass*)[[UIApplication sharedApplication] delegate];
UIWindow *mainWindow = appDelegate.window;

Resources