I am building an article reading app using storyboards.
I am also
using AMSlideMenu Library for the main menu. I am facing an problem that is, i want to
pass different url values when user click on different rows in AMSlideMenu.
here is my storyboad :
here is my code:
-(NSString *)segueIdentifierForIndexPathInLeftMenu:(NSIndexPath *)indexPath
{
NSString *idetifier = #"firstSegue";
// ysTableViewController *str;
switch (indexPath.row) {
case 0:
urlString = #"http://example.com/1";
break;
case 1:
urlString = #"http://example.com/2";
break;
case 2:
urlString = #"http://example.com/3";
break;
default:
urlString = #"http://example.com/4";
break;
}
return idetifier;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"firstSegue"])
{
ysTableViewController *controller = (ysTableViewController *)segue.destinationViewController;
NSLog(#"asgasfg-----%#",urlString);
controller.Mainlinks = urlString;
}
}
You can create a new UIViewController inside prepareForSegue method.
— YourTableViewController *newObj= [[YourTableViewController alloc]init];
- newObj.value=#"your value";
By This way u can pass your value.
I got over the problem by using NSUserDefaults temporarily but still it not a proper solution. Still looking for a proper method to pass parameters b/w classes.
Make a subclass of AMSlideMenuLeftTableViewController or AMSlideMenuRightTableViewController (if you don't have already),
and implement following method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *destNC = segue.destinationViewController;
UIViewController *destVC = nc.viewControllers.firstObject;
if ([segue.identifier isEqualToString:#"firstSegue"])
{
FirstViewController *vc = (FirstViewController *)destVC;
// pass your data to vc
// e.g.
vc.title = #"I'm from first row";
} else if ([segue.identifier isEqualToString:#"secondSegue"]) {
// same for others
//...
}
}
Related
I have made a segue passing a string which tells the next view controller which instance to parse the CoreData for. The problem is, I am using some code that calls init methods and the sent string is not initialized when it is called. However, the segue is working when I display the string in the destination view controller's viewDidLoad
- (id)init
{
self = [super init];
if (self)
{
[self initFakeData];
}
return self;
}
When that initFakeData method is called it sets up a graph and needs the exercise to hold a valid value
- (void)initFakeData
{
NSString *myExercise=exercise; //returns nil
if (myExercise==nil)
{
myExercise=#"Default";
}
}
Meanwhile...
-(void)viewDidLoad{
NSString *myExercise=exercise; //returns value
}
exercise is a property that is initialized by the previous view controller in a tableview
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if ([segue.identifier isEqualToString:#"showGraph"]) {
JBLineChartViewController *destViewController = segue.destinationViewController;
NSString *myExericse=[NSString stringWithFormat:#"%#", [[_exercises valueForKey:#"exercise"]objectAtIndex:indexPath.row]];
NSLog(#"%#",myExericse);
destViewController.exercise = myExericse;
}
}
The behaviour is correct because during init the exercise in JBLineChartViewController was not set. If you need the exercise attribute in the init method to set certain behaviour that have to be before viewDidLoad, my suggestion is to not use segue but do a designated initWithExercise and push the controller in code. Maybe like this:
- (IBAction)chartButtonPressed:(id)sender {
JBLineChartViewController *vc = [[ShopViewController alloc]initWithExercise:#"EXERCISE_STRING_HERE"];
[self showViewController:vc sender:self];
}
The new view controller is allocated and initialized before prepareForSegue is called. Anything you need to do with CoreData should be done in viewDidLoad. Or you can do it later, e.g. in viewWillAppear or viewDidAppear.
I am saving an image in my ViewController, and want to pass it to the next one during segue, however doing this causes an infinite loop.
Here is my code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"next"])
{
ReviewViewController *rvc = [segue destinationViewController];
[rvc setFilename:self.filename];
}
}
the setFilename method in ReviewViewController simply looks like this:
- (void)setFilename:(NSString *)filename{
NSLog([NSString stringWithFormat:#"Setting filename to: %#",filename]);
self.filename = filename;
}
That log statement fires off ad-infinitum when I add in the prepareForSegue method. If I remove it, the transition is fine.
What is going on?
The answer, as many pointed out in the comments, is that self.filename = actually calls [self setFilename], which is what I had called my method.
This is fixed by either renaming my setFilename method, or doing the assignment via _filename = instead.
I have passed button tag to another Viewcontroller.
It's passed but when I'm calling any another method like play audio play on that selected button, player not working...
I have tried below code :
It passes id of button clicked to the other view. Where I build player and and based on button id I want to play a poem on other view and control functions like volume control, progressbar, duration, play, push, next, etc...
While I'm giving a method to a particular button id to play poem it is not working...
This below code for play a poem in another view where I build player.
`
- (IBAction)btnpoemclicked:(id)sender {
btnPressed = [NSString stringWithFormat:#"%li", (long)[sender tag]];
NSLog(#"selected poem -%#",btnPressed);
PlayerController *pl=[[PlayerController alloc] init];
pl.btnPressed=btnPressed;
[pl setBtnPressed:pl.btnPressed];
[self performSegueWithIdentifier:#"player" sender:sender];
}
You should pass values in prepareForSegue when using Segues.
Update your code like
- (IBAction)btnPoemClicked:(id)sender {
btnPressed = [NSString stringWithFormat:#"%li", (long)[sender tag]];
[self performSegueWithIdentifier:#"player" sender:self];
}
And then In prepareForSegue pass the values to destinationViewController like this
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"player"]) {
PlayerController *playerController = (PlayerController *)segue.destinationViewController;
playerController.btnPressed = btnPressed;
}
}
Implement your button action like,
- (IBAction)buttonPoemClicked:(id)sender {
[self performSegueWithIdentifier:#"Player" sender:sender];
}
Pass your value by implementing prepareForSegue,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Player"]) {
PlayerController *playerController = (PlayerController *)segue.destinationViewController;
playerController.pressedButtonTag = [NSString stringWithFormat:#"%li", (long)[sender tag]];
}
}
Additional Note : Try to make your variable/class names little more understandable or try to follow apple recommended naming conventions.
i've been fixing this problem for a few days. but can't seem to get it..
help me out ..
let me explain my situation. Basically, i have navigation controller that contains table view controller and view controller. and i'm making simple phone book app.
And, i have a directory entry declared in extension class
#interface DetailViewController ()
#property DirectoryEntry *dirEntry;
#end
And, in table view, when you click the button it will transfer some data through segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailCV = [segue destinationViewController];
if ([segue.identifier isEqualToString:#"cellToDetail"]) {
[detailCV setDirEntry: [self.pbArray objectAtIndex:[self.tableView indexPathForSelectedRow].row]];
} else {
detailCV.dirEntry = nil;
}
//designate delegate !!!
detailCV.delegate = self;
}
My Problem occurs when it execute detailCV.dirEntry = nil; it will call my setter in viewController. it says EXC_BAD_ACCESS
-(void) setDirEntry:(DirectoryEntry *) dirEntry {
self.dirEntry = dirEntry;
}
Thank you in advance..
It's not an EXC_BAD_ACCESS so much as the OS killing your app for smashing the stack. This method is recursing infinitely:
-(void) setDirEntry:(DirectoryEntry *) dirEntry {
self.dirEntry = dirEntry;
}
Your use of dot notation expands to a setter which should make this more clear.
-(void) setDirEntry:(DirectoryEntry *) dirEntry {
[self setDirEntry:dirEntry];
}
Set the instance variable directly, or let the compiler handle it. Properties in class extensions are automatically synthesized.
I have very strange problem with my app. I'm using AFNetworking framework in my app. I'm loading some feed with images and onclick action open new view with detail description and big image. So I send IdPhoto to the description view, but when IdPhoto is more then 12 prepareForSegue send address in memory instead of IdPhoto value.
-(void)didSelectPhoto:(PhotoView*)sender {
//photo selected - show it full screen
[self performSegueWithIdentifier:#"ShowPhoto" sender:[NSNumber numberWithInt:sender.tag]];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([#"ShowPhoto" compare: segue.identifier]==NSOrderedSame) {
DescrController* streamPhotoScreen = segue.destinationViewController;
streamPhotoScreen.IdPhoto = sender;
}
}
part of PhotoView code
-(id)initWithIndex:(int)i andData:(NSDictionary*)data {
self = [super init];
if (self !=nil) {
//initialize
self.tag = [[data objectForKey:#"IdPhoto"] intValue];
so I couldn't understand why it doesn't send IdPhoto to another View when the value more then 12
Did you set your property streamPhotoScreen.IdPhoto as STRONG ? and be sure this is not a IBOutlet.
if 1. is ok, can you put a breakpoint in your prepareForSegue: to check if sender is really what you are waiting for...
I think you need to upcast/downcast(sender) to be appropriate for streamPhotoScreen.IdPhoto.