I am creating a news app that has title, description, video and details.
this is displayed on UIViewCell on TableView, when a user clicks on that
particular cell, corresponding news is displayed as shown below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NewsViewController *newsdetail = [[NewsViewController alloc] initWithNibName:#"NewsDetailView" bundle:nil];
self.newsdetails = detail;
[detail release];
newsdetails.title = [NSString stringWithFormat:#"%#",[titleData objectAtIndex:indexPath.row]];
newsdetails.itemID = [NSString stringWithFormat:#"%#",[NewsIds objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:newsdetails animated:YES];
}
The variables (which hold JSON data) are properly defined in NewsViewController and synthesised as follows
NSMutableArray *NewsIds;
NSMutableArray *titleData;
NSMutableArray *descriptionData; etc
Now, what I want is to add a video button as shown on the storyboard, so that a user can click it and be able to see details of corresponding video through modal /popup
any suggestions or help?
In the Storyboard, just use an Action segue on the UIButton and select modal. Control Drag from the button to the Video Details VC. Give that segue a name, let's say
playVideo
Then inside the prepareForSegue method in your PlayersViewController.m you just add this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"playVideo"]) {
// set properties on your destinationVieController to pass the data you need
}
}
Also use unwind segues on the buttons 'Cancel' and 'Done' in your destination VC to return to the previous TVC. Alternatively you could also use dismissViewControllerAnimated: that will be passed to the calling VC.
you should have a custom table view cell which will be handling actions and data on cell.
Related
I have a UITableView with a custom table cell which has several controls on it - one of them is a button.
When clicking the cell itself I'm sending some data via a segue to a view controller - that works fine.
I want to open a different view controller when the button form inside the cell is clicked - and again, send some data.
The button is connected to the view controller which is opening fine when the button is clicked.
The problem is I can't get the IndexPath inside the prepareForSegue in order to set the parameter I wish to send to the view controller.
This code brings the first object from the table datasource:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"CommentSegue"])
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSDictionary *detail = [myObject objectAtIndex:path.row];
CommentViewController *navController = segue.destinationViewController;
navController.data=detail;
}
}
While I want the n item which the button belongs to...
Thanks.
You should never call a segue from within the cell itself.
You have to define a target for the button in -tableView: cellForRowAtIndexPath: when populating the data for the cell.
do this:
[cell.theButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
and to get the index of the row that button belongs to, use this trick:
cell.theButton.tag = indexPath.row
you can then handle the click on your table view controller like this:
-(void) buttonClicked:(UIButton *) button
{
NSInteger index = button.tag;
// now prepare the data and perform segue
}
My storyboard application flows like this:
When a button(let me call it "BtnA") is clicked it loads a table view(Let me call it "TblA").
Once clicked on particular cell, a corresponding image("ImgA") will be loaded in a image view.
On image view's top bar there is another button(let me call it "BtnB").When I click on the button(BtnB) one table view(Let me call it "TblB") as popover is loaded; And, its cell values as same as the previously loaded tableview("TblA") cell(that is tableview loaded after my initial button click).
Now, What I want is, when popover tableview("TblB") cell is selected I want to load my corresponding image("ImgA") back to the image view.How Can I do this?
Note: .Button("BtnB") is connected tableview("TblB") as popover segue.
I was referring to this(load images in an image view from a table in a popover ), but, its seems that its bit different as its loading tableview's cell image to the image view.
Hope my question explanation is understandable.Please help.
You can use delegation in this case. What you can is, create a protocol in TblB controller
#protocol TblBDelegate <NSObject>
-(void)didSelectCellWithImage:(UIImage*)image
#end
I passed the image here, but you can pass whatever you need.
You create a delegate property in TblB
#property (nonatomic, assign) id< TblBDelegate> delegate
Now you said you are presenting TblB popover with a segue. So in TblA, you implement prepareForSegue method and set TblA controller as the delegate
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"SEGUE NAME"]) {
TblBController *controller = (TblBController*)[segue destinationViewController];
controller.delegate = self;
}
}
Now in your TblA controller, you conform to protocol TblBDelegate, and implement the method
#interface TblAController <TblBDelegate>
#end
and in your .m file
-(void)didSelectCellWithImage:(UIImage*)image
{
// Update your imageView with new image
}
and in your TblB controller
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get selected image
// and call your delegate method
if (self.delegate && [self.delegate respondsToSelector:#selector(didSelectCellWithImage:)]) {
[self.delegate didSelectCellWithImage:selectedImage]
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
I want to create table, and when click cell in table , the content will display in another view. Each of table has different design. How do I do that.
I don't enough to post image, you can see in this link. thanks.
Use this delegate method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"InstructionToSection" sender:nil]; //"InstructionToSection" is seque name
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
InstructionSectionViewController * instructSection = [segue destinationViewController]; // InstructionSectionViewController is ur next viewcontroller. 1st u need to import your destination viewcontroller in header
instructSection.SelectedItem = #"Ur Passing value"; // selectedItem should be declare in InstructionSectionViewController as property, need to synthesize
}
For more detail refer to this link
I'm totally lost in this issue. I have been working with storyboards, I have created a navigation controller with tableviews and some stuff. There are Services in each row of the tableview and I need to create one detail view for each service.
There are a lot of services, so I can't create them in the storyboard. The idea is to download the Services from a webservice (number of parameters, types of each one, etc..) and add as textfields / buttons as appropriate to the Service.
So, my problems and questions are:
1) Can I combine Storyboards and views programmatically? When I create a NewView in MyTableviewClass, should I do it in my prepareforsegue method? How can I show it in the screen without loosing my navigation controller? this is what I have (it doesn't work: it says to me that there is no segue with name 'Service1' ) :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if ([[segue identifier] isEqualToString:#"NextLevel"]) {
[segue.destinationViewController setActualNodo:[actualNodo getSonAtIndex:indexPath.row]];
} else if ([[segue identifier] isEqualToString:#"Service1"]) {
CGRect bounds = self.view.bounds;
UIView *myview = [[UIView alloc] initWithFrame:bounds];
[myview setBackgroundColor: [UIColor redColor]];
[self.view addSubview:myview];
[self.view bringSubviewToFront:myview]; }
Any book or reference is welcomed but I couldn't find anything similar. Is this very complicated in iOS? I have done a similar thing in Java. I have read about generating interfaces dynamically with XIBs but, sincerely, I don't know what it is..
Thanks for all.
Yes you can create a StoryBoard with a view and then add views programmatically to it.
You should not try creating a view within your prepareForSegue method. This really should be used for passing objects to another ViewController.
I would suggest this to you. Go back to your StoryBoard and create a new UIViewController scene. Click on your first scene and CTRL drag to the new scene. Next, click on your segue and give it a name.
Step 1:
Create a new class called ServicesViewController and make sure it's a subclass of `UIViewController:
Step 2:
Go back to your StoryBoard and click on scene so that it is selected. Next, click on the Files Owner and finally click on the class info button (the third button) and finally select your ServiceViewController class you just created.
Step 3:
Back in your ServicesViewController in the didSelectRowAtIndex method call your seque:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME" sender:nil];
}
For now, clean out all the code in your prepareForSegue method and just get the transition down first.
In addition to Flea's answer, if you need to keep the navigation controller, just create a push segue in your storyboard by control dragging from the file owner icon (the yellow box under your view controller's view) of the table view controller to the ServiceViewController you added to the storyboard, this should show a popup window where you can select "push" as the type of the segue. Next, select the segue and in the attribute inspector (the fourth button, next to the one in the snapshot) and in the "Identifier" text field type in a unique identifier for your segue, such as serviceSegue.
At this point, using Flea's code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"serviceSegue" sender:nil];
}
And in the code you posted:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
if ([[segue identifier] isEqualToString:#"serviceSegue"])
[segue.destinationViewController setActualNodo:[actualNodo getSonAtIndex:indexPath.row]];
}
I'm not sure what you are trying to do with the other segue "Service1", but if you want to change the view of the TableViewController, segues are not the way to do it. If anything you should do it in the didSelectRowAtIndexPath method depending on the row selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (You want to transition to other view controller)
[self performSegueWithIdentifier:#"serviceSegue" sender:nil];
else
Change your view here.
}
I hope this helps!
It sounds like you just have multiple cells in a tableView. Instead of using segues you can simply create different cells with different identifiers and show or hide them based on what services are detected in your services array which is populated from your web service.
I have a storyboard with tabbarcontroller. One of tab bar has a tableview and I want that when the user tap in a row from tableview open a detail view. The problem is when I open detail view tab bar and navigation bar hides... In the storyboard I create the detail view as a new view controller, then I create a new file and referred it to the class of detail view .
The code in didselectrowatindexpath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
detalleYouTube *dvController = [[detalleYouTube alloc] init];
[self.navigationController pushViewController:dvController animated:YES];
}
Thank you in advance!
This is kinda old but if someone needs to do this here's an easy approach:
You can use add a segue from the view in the tab bar to detalleYouTube, put an identifier to the segue and do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"segueIdentifier" sender:tableView];
}
Another approach to this is not to use tableView:didSelectRowAtIndexPath but instead use prepareForSegue:sender
the way I did it was:
-(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
DetailViewController *viewController = [segue destinationViewController];
CustomObject *custObject = [arrayOfObjects objectAtIndex:[self.tableView indexPathForSelectedRow].row];
viewController.objectNeeded = custObject;
}
This example is based on the idea that your detail view controller is connected to your table view controller.
I presume you have the 'Detail' view as part of the storyboard (not in a separate XIB), if so you will need to place a separate NavigationController at the start of the 'Detail' TabBarItem seque.
This page has a good tutorial on what I think your trying to achieve:
http://maybelost.com/2011/10/tutorial-storyboard-in-xcode-4-2-with-navigation-controller-and-tabbar-controller-part1/
Also check these links to a more in-depth Storyboard tutorial:
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2