I am at a loss as to how to pass a selected image in one view controller to a tab bar controller using a segue, i keep on getting "unrecognized selector sent to instance ...". Here is my prepare for segue in a ViewController3 :
if ([[segue identifier]isEqualToString:#"tabbarGo"]) {
UITabBarController *tabar=segue.destinationViewController;
UINavigationController *navController = [tabar.viewControllers objectAtIndex:1];
ViewController5 *fifthView=[navController.viewControllers objectAtIndex:0];
fifthView.theImage = selectedImage.image;
[tabar setSelectedIndex:1];
}
i have imported ViewController5 into ViewController3 as 5 is the tab bar controller and the m file for viewcontroller5 is as follows :
#import <UIKit/UIKit.h>
#import "ViewController5.h"
#import "ViewController3.h"
#interface ViewController5 ()
#end
#implementation ViewController5
#synthesize theImage;
-(void)setImage:(UIImage *)image
{
}
- (void)viewDidLoad {
[super viewDidLoad];
[_imageView setImage:theImage];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I do not understand what i am doing wrong and have gone through most of the posts, or should i go through a "NSUserDefaults *prefs" approach to this ?
Any help appreciated and thanks.
ok got it to work to send an image to a tab bar with navigation controller by adding one line of code, i was forgetting to specify the NSString value of the segue so here is the final full code for "prepare for segue" :
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString * segueIdentifier = [segue identifier];
UITabBarController *tabar=segue.destinationViewController;
UINavigationController *navController = [tabar.viewControllers objectAtIndex:1];
ViewController5 *fifthView=[navController.viewControllers objectAtIndex:0];
fifthView.theImage = selectedImage.image;
[tabar setSelectedIndex:1];
}
Related
I have a storyboard scene that is a UITabBarController scene and it has about 5 tab bar items. What I am trying to do is remove an item or two based on the user's bundle settings. So I created a UITabBarController .h and .m file like so:
.h:
#import <UIKit/UIKit.h>
#interface LHTabBarController : UITabBarController
#end
.h:
#import <Foundation/Foundation.h>
#import "LHTabBarController.h"
#implementation LHTabBarController
-(void)viewDidLoad
{
/*NSMutableArray *tabbarViewControllers = [NSMutableArray arrayWithArray: [self.tabBarController viewControllers]];
[tabbarViewControllers removeObjectAtIndex:1];
[self.tabBarController setViewControllers: tabbarViewControllers];*/
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[super viewDidAppear:animated];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
and I connected this class to the UITabBarController in my storyboard.
I tried the commented-out code, but that gave me an array saying the array was empty.
How do I remove the tab bar item from this class?
Simply do this:
As you are doing this on Tab Controller, simply state self than self.tabBarController
NSArray *actualItems= self.viewControllers;
NSMutableArray *array=[[NSMutableArray alloc]initWithArray:actualItems];
[array removeObjectAtIndex:0];
[self setViewControllers:array animated:YES];
I'm new to iOS and Its developing.i have read Xcode old style passing data between view controllers.there i have seen lot of things going with xib files.please find below the code i have used to pass data in-between ViewControllers.but there in customelink(please find DetailViewController.m)there I'm getting Null value.please help me to get String value there.and kind enough to explain what are the mistakes I've done here.
EssentialInfoController.h
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
#interface Essentialinfocontroller : UIViewController
#end
EssentialInfoController.m
#import "Essentialinfocontroller.h"
#interface Essentialinfocontroller ()
#end
#implementation Essentialinfocontroller
- (void)viewDidLoad
{
[super viewDidLoad];
DetailViewController * customelink= [DetailViewController alloc ];
customelink.link=#"https://www.facebook.com";
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property(weak,nonatomic)NSString * link;
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#property(nonatomic,weak)NSString *customelink;
#end
#implementation DetailViewController
#synthesize link;
#synthesize customelink;
- (void)viewDidLoad
{
[super viewDidLoad];
self.customelink=self.link;
NSLog(#"link--> %#",customelink);// here 2014-07-06 21:21:51.469 WADTourisum[880:60b] link--> (null)
}
#end
DetailViewController * customelink= [DetailViewController alloc ];
should be
DetailViewController * customelink= [[DetailViewController alloc ] init];
and
#property(weak,nonatomic)NSString * link;
should be
#property(strong,nonatomic)NSString * link;
and log it
NSLog(#"link--> %#",self.link);
you don't need this one
#property(nonatomic,weak)NSString *customelink;
if you use storyboard and segue then you have to implement
-(void)prepareForSegue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
assume that you got a button to move from essentialVC to detailVC, in you IBAction, call this method:
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME_HERE" sender:self];
this is just a brief answer. I suggest you read about UINavigationController first, it's much easier to use compared to segue, IMHO.
I have a drawing app with begins with choosing a category. Every category has his own drawing template. How do I get the secondViewController, to load the correct template? The category is chosen true a button from the firstViewController.
I have managed to give each segue a own identifier. And then what?
I only find examples of ways to parse data or images. But I don't want it to parse. I want it to load the image only on the secondViewController.
SecondViewController.h File
#property (strong, nonatomic) UIImage *theCatImage;
SecondViewController.m File
#interface secondViewController()
#property (weak, nonatomic) IBOutlet UIImageView *image;
#end
#implementation secondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.image.image = self.theCatImage;
}
ViewController.h File
#import <UIKit/UIKit.h>
#import "cat01ViewController.h"
#interface ikzieikzieAppViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *newImage;
#end
ViewController.m File
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"vervoer"]) {
cat01ViewController *secondView = (cat01ViewController *)segue.destinationViewController;
// dont know what to send here... i tried to hide and show a image like this;
secondView.theCatImage = self.newImage;
}
}
EDIT
Above code doesn't work, ALSO i don't want to send a image from one to another viewcontroller.
This is what i want;
On the firstViewController, a user can choose from four different categories. Each categorie will show his own drawing template and also, gives his own ID when the drawing is send to a parse database at the end.
So, what is the best way to set this up?
***SOLUTION***
Eventually, this is the way i solved it.
ViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"vervoer"]) {
UIImage *image = [UIImage imageNamed:#"vervoer.png"];
cat01ViewController *vc = [segue destinationViewController];
vc.showImage = image;
}
else if ([segue.identifier isEqualToString:#"huis"]) {
UIImage *image = [UIImage imageNamed:#"huis.png"];
cat01ViewController *vc = [segue destinationViewController];
vc.showImage = image;
}
else if ([segue.identifier isEqualToString:#"mode"]) {
UIImage *image = [UIImage imageNamed:#"vinkje.png"];
cat01ViewController *vc = [segue destinationViewController];
vc.showImage = image;
}
else if ([segue.identifier isEqualToString:#"eten"]) {
UIImage *image = [UIImage imageNamed:#"bord.png"];
cat01ViewController *vc = [segue destinationViewController];
vc.showImage = image;
}
}
SecondViewController
#synthesize catImage, showImage;
- (void)viewDidLoad
{
[catImage setImage:showImage];
[super viewDidLoad];
}
What you want to do is to add a property on SecondViewController:
#property (nonatomic) int category;
Then inside FirstViewController
Add property
#property (nonatomic) int chosenCategory;
Set this to the category that the user selected.
Then inside your seque code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
cat01ViewController *secondView = (cat01ViewController *)segue.destinationViewController;
[secondView setCategory:self.chosenCategory];
}
This will tell the VC which image it will need to draw. Then you call your parse library method to retrieve the image based on the ID (Category) chosen inside the viewDidLoad method in SecondViewController:
SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.image.image = [ParseLibrary getImageForCategory:self.chosenCategory];
}
I have three navigation buttons in a view controller, two of which I process locally and one which launches an edit view controller. I have created a segue from the body of the first UITableView to the Navigation Controller of the destination view controller (the edit view controller).
The button works fine (i.e. launches the edit view) but it doesn't receive the string I send to it. Could someone please help. I have tried all of the suggestions given to other related questions but cannot seem to fix this. Following is the related code.
Source controller.h:
#import <UIKit/UIKit.h>
#import "EQHorseAddViewController.h"
#import "EQDatabase.h"
#interface EQHorseDetailViewController : UITableViewController
#property (strong, nonatomic) EQHorseAddViewController *addViewController;
Source controller.m:
#import "EQHorseDetailViewController.h"
#interface EQHorseDetailViewController ()
#end
#implementation EQHorseDetailViewController
#synthesize addViewController;
-(void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *deleteItem= [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteHorse:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self
action:#selector(editHorse:)];
NSArray *actionButtonItems = [[NSArray alloc] initWithObjects:deleteItem, editItem,
nil];
self.navigationItem.rightBarButtonItems = actionButtonItems;
}
-(void)editHorse:(id)sender
{
NSLog(#"EditingHorse");
self.addViewController= [[EQHorseAddViewController alloc] init];
addViewController.resultSegue = #"Edit Horse Details";
[self performSegueWithIdentifier:#"EditHorseDetails" sender: self];
}
Destination View Controller.h:
#import <UIKit/UIKit.h>
#import "EQHorseDetailsInfo.h"
#import "EQDatabase.h"
#class EQHorseAddViewController;
#protocol EQHorseAddViewControllerDelegate <NSObject>
- (void) eqHorseAddViewControllerDidCancel:(EQHorseAddViewController *)controller;
- (void)eqHorseAddViewControllerDidSave: (EQHorseAddViewController *)controller;
#end
#interface EQHorseAddViewController : UITableViewController
<EQHorseAddViewControllerDelegate>
#property (strong, nonatomic) EQHorseAddViewController *addViewController;
#property (nonatomic, weak) id <EQHorseAddViewControllerDelegate> delegate;
#property (strong,nonatomic) NSString *resultSegue;
Destination View Controller.m
#import "EQHorseAddViewController.h"
#import "EQDatabase.h"
#import "EQHorseDetailsInfo.h"
#interface EQHorseAddViewController ()
#end
#implementation EQHorseAddViewController
#synthesize resultSegue;
- (void)viewDidLoad
{
NSLog(#"resultSegue %#",resultSegue);
}
Thanking you in advance.
It looks like you're referring to the code in editHorse:, which allocates an EQHorseAddViewController and sets a property on it. The problem with that code is that the view controller it allocates bears no relation to the vc destination of the segue. That method simply creates a vc and then instantly discards it.
Remove the allocation and instead implement prepareForSegue:. The segue object passed to that method will have a property called destinationViewController. That's the view controller that's about to be pushed onto the navigation. That's the one on which you should set the resultSegue property.
-(void)editHorse:(id)sender
{
NSLog(#"EditingHorse");
[self performSegueWithIdentifier:#"EditHorseDetails" sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"EditHorseDetails"]) {
EQHorseAddViewController *vc = segue.destinationViewController;
vc.resultSegue = #"Edit Horse Details";
}
}
You need to define the new controller as follows:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EQHorseAddViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"EditHorseDetails"];
ivc.resultSegue = #"Edit Horse Details";
Perhaps you'll need this method too
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"EditHorseDetails"]) {
// Get destination view
EQHorseAddViewController *vc = [segue destinationViewController];
vc.resultSegue = #"Edit Horse Details";
}
}
-(void)editHorse:(id)sender
{
NSLog(#"EditingHorse");
resultSegue = #"Edit Horse Details";
[self performSegueWithIdentifier:#"EditHorseDetails" sender: self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *navigationController = segue.destinationViewController;
EQHorseAddViewController *addViewController = [navigationController viewControllers]
[0];
// Get destination view
NSLog(#"ResultSegue in Prepare - %#",resultSegue);
addViewController.resultSegue = #"Edit Horse Details";
}
I'm trying to learn more about Objective C blocks and how they work. I've set up a simple project with two UIViewControllers embedded in a UINavigationController in Storyboard. I'm attempting to change the background color of the first ViewController's view from the second view controller. Here's some code:
ViewController.m
#implementation ViewController{
ColorBlock _colorBlock;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"theSegue"]){
SecondViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
vc.colorBlock = _colorBlock;
}
}
- (IBAction)moveToSecondViewController:(id)sender {
__weak id weakSelf = self;
_colorBlock = ^{
[[weakSelf view] setBackgroundColor:[UIColor redColor]];
};
}
SecondViewController.h
typedef void (^ColorBlock)(void);
#interface SecondViewController : UIViewController
#property (readwrite, copy) ColorBlock colorBlock;
#end
SecondViewController.m
- (IBAction)buttonTapped:(id)sender {
if(self.colorBlock){
self.colorBlock();
}
}
The first ViewController's background color isn't being changed because in the buttonTapped: method of SecondViewController.m, self.colorBlock is nil, causing the block invocation not to be called. I thought I had successfully set the block in prepareForSegue:sender:. Why is my block property nil?
In your prepareForSegue, the destination has already been instantiated. So assuming that SecondViewController is the destination, you can do:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"theSegue"]){
SecondViewController *vc = segue.destinationViewController;
NSAssert([vc isKindOfClass:[SecondViewController class]], #"destination is not SecondViewController class");
vc.colorBlock = _colorBlock;
}
}