Crash switching views when adding data to a property - ios

When trying to switch views (minus the two lines that add data to a property, it works fine. However with the 2 lines in (which is these two):
self.firstViewData = fvc;
firstViewData.passedData = #"hello test test test";
It crashes saying:
2013-05-29 16:40:43.864 test [16166:907] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setPassedData:]: unrecognized selector sent to instance 0x325620'*
Whole segment:
FilterViewController.h
#interface FilterViewController : UIViewController
{
FirstViewController *firstViewData;
}
#property (nonatomic, retain) FirstViewController *firstViewData;
FilterViewController.m
#synthesize firstViewData;
- (IBAction)backToMap:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
FirstViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
fvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
self.firstViewData = fvc;
firstViewData.passedData = #"hello test test test";
[self presentViewController:fvc animated:YES completion:nil];
}
FirstViewController.h
#interface FirstViewController : UIViewController
{
NSString *passedData;
}
#property(nonatomic, retain) NSString *passedData;
FirstViewController.m
#synthesize passedData;
NSLog(#"result: %#", passedData);

Your fvc variable is a UITabBarController, not a FirstViewController. Look into how you set fvc.

Related

How to pass data between view controller in objective-c?

The select item is a slider. When I click the slider It will pass the data to Second VC (WebViewController). but how to pass data from first view controller to second view controller in objective-c? Sorry, This is my first time coding objective C.
First VC .m file
#import "WebViewController.h"
- (void)viewDidLoad {
[super viewDidLoad];
arraySliderProducts = [[NSMutableArray alloc]init];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
UIViewController *controller = nil;
switch (indexPath.row)
{
case 0:
{
WebViewController *WebViewController = [[WebViewController alloc] init];
//error: No visible #interface for "WebViewController" declares the selector 'alloc'
WebViewController.data = arraySliderProducts[indexPath.row][#"title"]; //pass this link value
//error: Property 'data' not found on object of type 'WebViewController'
[self.navigationController pushViewController: WebViewController animated:YES];
}..
Second VC .m file
#interface WebViewController ()
#property (nonatomic, retain) NSString *data;
Second VC .h file
#import <UIKit/UIKit.h>
#interface WebViewController : UIViewController
{
AppDelegate *appDelegate;
NSString *data;
}
#end
Two points to note:
1: This is not the way to initiate a view controller to show/present in your app.
2: You should declare your NSString *data in your SecondVC's .h file.
Now point 1 solutions is to change your code with below in your didSelectItemAtIndexPath: function
switch (indexPath.row)
{
case 0:
{
// By Default storyboard name is "Main". Change it if you you have different name in your project
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
// Now instantiate your view controller with its identifier on this storyboard object.
// Note: don't forget to set viewControllers' identifier
WebViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier: #"WebViewController"];
// Now Pass your value
WebViewController.data = arraySliderProducts[indexPath.row][#"title"];
//Finally push this object on your navigation stack
[self.navigationController pushViewController: WebViewController animated:YES];
}...
}
try as
//Your class and obj name is same so You are getting this type of error
WebViewController *VC = [[WebViewController alloc] init];
//OR
WebViewController *VC= [self.storyboard instantiateViewControllerWithIdentifier:#"WebViewController"];
VC.data = arraySliderProducts[indexPath.row][#"title"];
[self.navigationController pushViewController: VC animated:YES];

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?

Crash when set value to parent controller in Singleton

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

Core data and Master-Detail template, getting runtime exception.

I am trying to modify Master Detail project template generated by xcode.
But getting following problem:
2013-08-03 20:08:59.749 StudentsAtWork[20236:a0b] Unknown class cblMasterViewController in Interface Builder file.
2013-08-03 20:08:59.900 StudentsAtWork[20236:a0b] -[UITableViewController setManagedObjectContext:]: unrecognized selector sent to instance 0x8ec8120
2013-08-03 20:08:59.904 StudentsAtWork[20236:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewController setManagedObjectContext:]: unrecognized selector sent to instance 0x8ec8120'
*** First throw call stack:
(0x1a009b8 0x17818b6 0x1a9cc13 0x19f0cfb 0x19f08de 0x61ee 0x57bea9 0x57c6e9 0x57db5e 0x593a6c 0x593fd9 0x57f7d5 0x38ce906 0x38ce411 0x197c3e5 0x197c11b 0x19a6b30 0x19a610d 0x19a5f3b 0x57d2b1 0x57f4eb 0x6d7d 0x2060725)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
What I did basically, removed the generated master and detail view controller class and created my own MasterViewController class like following:
header file:
#import <UIKit/UIKit.h>
#interface cblMasterViewController : UITableViewController
#property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;
#end
Implementation file:
#import "cblMasterViewController.h"
#interface cblMasterViewController ()
#end
#implementation cblMasterViewController
#synthesize managedObjectContext;
....
....
#end
and my app delegate code is as follows:
#import "cblAppDelegate.h"
#import "cblMasterViewController.h"
#implementation cblAppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
cblMasterViewController *controller = (cblMasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
} else {
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
cblMasterViewController *controller = (cblMasterViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
//}
return YES;
}
Also I have deleted detail view from my storyboard.
I am new in using storyboard. So I am not sure what is going wrong here.
Can anyone give me some clue?
Thanks.

[UITabBarController setPassedData:]: unrecognized selector sent to instance

I will give a outline of the program below but, when pushing a new view controller (not a tab bar view) I select a value on a stepper and then click a button to go back. I am trying to send the stepper value to the FirstViewController from FilterViewController.
The program:
App loads on FirstViewController which is the first tab of a tab bar controller, in the top left of the screen is a button (magnifying glass) which opens up FilterViewController.
FilterViewController has a stepper, a label (which displays the value of the stepper) and a button. You click the button, it saves to a variable the value of the stepper and I need to pass it to FirstViewController.
FirstViewController.h (the property im accessing from FilterViewController)
#interface FirstViewController : UIViewController
{
NSString *passedData;
}
#property(nonatomic, retain) NSString *passedData;
FirstViewController.m (code that pushes the new view controller, the property is also synthesised in the implementation)
- (IBAction)searchOptions:(id)sender {
FilterViewController *ctrl = [[FilterViewController alloc] init];
[UIView transitionFromView:self.view toView:ctrl.view duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:nil];
self.filterViewController = ctrl;
[self.navigationController pushViewController:self.filterViewController animated:NO];
}
FilterViewController.m (code that saves the value and passes it to FirstViewController)
- (IBAction)backToMap:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
FirstViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:#"TabBarController"];
fvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
self.firstViewData = fvc;
fvc.passedData = #"yo";
//firstViewData.passedData = #"hello test test test";
[self presentViewController:fvc animated:YES completion:nil];
}
When it gets to the execution of this it crashes saying: [UITabBarController setPassedData:]: unrecognized selector sent to instance
Assuming you mean that the crash happens in following line of your code
fvc.passedData = #"yo";
you may want to check that fvc is indeed a FirstViewController NSLog("%#", fvc); i.e. was the class in IB changed from a UIViewController to a FirstViewController ?
Look at your code. You are giving TabBarController as identifier for FirstViewController. May be you are mistaken
FirstViewController *fvc = [storyboard instantiateViewControllerWithIdentifier:#"TabBarController"]; // Here check the identifier
fvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
self.firstViewData = fvc;

Resources