I'm having trouble implementing a toolbar (below my navigationbar) and a tableview. In my storyboard I created a normal ViewController, then made it part of the NavigationController, then added a UIToolBar and a UITableView. After this was done, I created the ViewController files. It all works. However, when I press a button in the toolbar it is being registered as a click on a cell and thus crashing the app. My knowledge isn't sufficient enough for me to be able to fix this, that's why I'm asking here.
Furthermore, if you scroll the toolbar doesn't stay at the top. It scrolls along with the table.
How can I fix these two problems?
#import "TableViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+AFNetworking.h"
#import "Ninja.h"
#import "DetailViewController.h"
#import "AMSlideMenuMainViewController.h"
#import "UIViewController+AMSlideMenu.h"
#import "UIImageView+WebCache.h"
#import "NSString+FontAwesome.h"
#interface TableViewController ()
#property (nonatomic, strong)NSArray *ninjas;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *movies;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *shows;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *profile;
#property (weak, nonatomic) IBOutlet UIToolbar *mainToolBar;
#end
#implementation TableViewController
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Your watched movies";
[self addRightMenuButton];
[self loadNinjas];
[self.movies setTitleTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"FontAwesome" size:24.0],
NSForegroundColorAttributeName: self.view.tintColor
} forState:UIControlStateNormal];
[self.movies setTitle:[NSString fontAwesomeIconStringForIconIdentifier:#"fa-dot-circle-o"]];
[self.shows setTitleTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"FontAwesome" size:24.0],
NSForegroundColorAttributeName: self.view.tintColor
} forState:UIControlStateNormal];
[self.shows setTitle:[NSString fontAwesomeIconStringForIconIdentifier:#"fa-pencil-square-o"]];
[self.profile setTitleTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"FontAwesome" size:24.0],
NSForegroundColorAttributeName: self.view.tintColor
} forState:UIControlStateNormal];
[self.profile setTitle:[NSString fontAwesomeIconStringForIconIdentifier:#"fa-plus"]];
self.mainToolBar.barTintColor = [UIColor whiteColor];
self.mainToolBar.layer.shadowColor = [[UIColor blackColor] CGColor];
self.mainToolBar.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.mainToolBar.layer.shadowRadius = 3.0f;
self.mainToolBar.layer.shadowOpacity = 1.0f;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.ninjas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
cell.textLabel.text = [self.ninjas[indexPath.row] name];
NSString *imageUrl = [NSString stringWithFormat: #"%#", [self.ninjas[indexPath.row] thumbnail]];
[cell.imageView setImageWithURL:[NSURL URLWithString:imageUrl]
placeholderImage:[UIImage imageNamed:#"50-50.jpg"]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50.0f;
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
DetailViewController *detailvc = [segue destinationViewController];
// Pass the selected object to the new view controller.
NSIndexPath *index = self.tableView.indexPathForSelectedRow;
Ninja *ninja = self.ninjas[index.row];
detailvc.ninja = ninja;
}
- (void)loadNinjas {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com/movies"]];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
request = [mutableRequest copy];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *jsonArray = (NSArray *)[responseObject objectForKey:#"data"];
NSMutableArray *tempNinjas = [[NSMutableArray alloc] init];
for (NSDictionary *dic in jsonArray) {
Ninja *ninja = [[Ninja alloc] initWithDictionary:dic];
[tempNinjas addObject:ninja];
}
self.ninjas = [[NSArray alloc] initWithArray:tempNinjas];
tempNinjas = nil;
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error Retrieving Shows"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
}
#end
And this is the storyboard screenshot: http://imgur.com/EOe9iQ3
It seems that you have UITableViewController as your view controller, which always has UITableView as its main view and that can't be changed.
What you could do is just drag new UIViewController onto your storyboard, which will come with usual UIView as its main view. Then cut/paste your existing table and toolbar views as your new UIView subviews.
Since toolbar now isn't a subview of your table view, it'll never scroll with it nor will it have any crashes upon taps on its items.
Finally, connect any relevant outlets/actions from the views of your new UIViewController to your new view controller, and don't forget to set the latter as a delegate/data source for your table view (which is done automatically for UITableViewController)
Related
Trying to load a url being passed from the previous viewcontroller in a tabviewcontroller sequence. Checked the usual suspects. UIWebview is added view outlet. All the proper delegate are connected. I'm not sure if the NSURL is being passed to the next view controller as I get a black screen (although the response object isn't nil). It's driving me crazy because it's a simple problem and it's something I've done millions of times but it's not working. Any help would be appreciated.
Below you'll find the code.
SquirrelInformationTableViewController.m
#interface ASAInformationTabViewController (){
NSMutableArray *squirrelArray;
}
#property (nonatomic, strong) ODRefreshControl *refreshControl;
#property (weak, nonatomic) IBOutlet UITableView *squirrelListTableView;
#end
#implementation ASAInformationTabViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpSquirrelList];
//Add drag to refresh function
self.refreshControl = [[ODRefreshControl alloc]initInScrollView:self.squirrelListTableView];
self.refreshControl.tintColor = [UIColor colorWithRed:46.0/255 green:172.0/255 blue:247.0/255 alpha:1];
[self.refreshControl addTarget:self action:#selector(dragToRefreshAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)setUpSquirrelList
{
__weak UITableView* weaktable = self.squirrelListTableView;
[[ASASquirrelManager instance] listSquirrels:^(NSString *response, NSMutableArray *result) {
if ([response isEqualToString:CODE_SUCCESS]) {
squirrelArray = result;
dispatch_async(dispatch_get_main_queue(), ^{
if (weaktable) {
[weaktable reloadData];
}
});
}
}];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ASASquirrelInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"squirrelIdentifier"];
ASASquirrelModel *squirrelLocal = squirrelArray[indexPath.row];
cell.squirrelTitleLabel.text = squirrelLocal.title;
cell.squirrelDescLabel.text = squirrelLocal.desc;
NSData *imageData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:squirrelLocal.image]];
cell.squirrelImage.image = [UIImage imageWithData:imageData];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ASASquirrelModel *squirrelWebLocal = squirrelArray[indexPath.row];
NSString *squirrelURLString = squirrelWebLocal.url;
//create an array with the string you want an access it
NSLog(#"%#", urlStringArray);
//Create a string from the URL pass it to the sdvc.string
SquirrelDetailViewController *sdvc = [SquirrelDetailViewController new];
sdvc.urlString = squirrelURLString;
[self.navigationController pushViewController:sdvc animated:YES];
}
SquirrelDetailViewController.m
#interface SquirrelDetailViewController () <UIWebViewDelegate>
#end
#implementation SquirrelDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.frame = self.view.bounds;
[self.view addSubview:self.webView];
}
-(void)viewWillAppear:(BOOL)animated
{
self.urlstr = [NSURL URLWithString:self.urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.urlstr];
[self.webView loadRequest:requestObj];
}
I think you need to create the instance of the sdvc using storyboard otherwise it will not be getting instantiated properly. So:
SquirrelDetailViewController *sdvc =
(SquirrelDetailViewController *)[[UIStoryboard storyboardWithName:#"XXX" bundle:nil] instantiateViewControllerWithIdentifier:#"YYY"];
where XXX is your storyboard name and YYY is the storyboard identifier for SquirrelDetailViewController.
I found out that the trouble was I wasn't preparing for segue. (hits forehead*)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"webviewpush" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"webviewpush"]) {
// Get destination view
SquirrelDetailViewController *vc = [segue destinationViewController];
NSIndexPath *path = [self.squirrelListTableView indexPathForSelectedRow];
ASASquirrelModel *squirrelWebLocal = squirrelArray[path.row];
NSString *squirrelURLString = squirrelWebLocal.url;
vc.urlString = squirrelURLString;
}
}
If you'll excuse me I'm going to go drown myself now.
I have two viewControllers where one of them contains a UILabel and a UIButton, and the other one a UITableView. When the UIButton in the first view is pressed the new ViewController is presented and when a row in the tableview is selected the tableview is dismissed and the UILabel in the first viewController should be updated so its text is the same as the cell name tapped in the tableview.
Code:
In the first Viewcontroller:
- (void)changeTitleWithString:(NSString *)title
{
UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)];
[selected_station setFont:[UIFont systemFontOfSize:16.0]];
selected_station.textAlignment = NSTextAlignmentCenter;
selected_station.text = [NSString stringWithFormat:#"%#", title];
[self.view addSubview:selected_station];
NSLog(#"%#", selected_station);
NSLog(#"%#", title);
}
In second viewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.currentString = [NSString stringWithFormat:#"Cell %d", indexPath.row+1];
NewViewController *viewCOntroller = [[NewViewController alloc] init];
[viewController changeTitleWithString:self.currentString];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
The changeTitleWithString function is called and the string from tableview is successfully received in the function, but the UILabel won't show up. When I log out the UILabel (selected_station) it's not nil and its text is correct. Why doesn't the string show up on the view?
This does not look like you are referencing back to a previous allocated view controller (NewViewController). Rather you are allocating a new NewViewController.
A better approach might be to create a delegate protocol for the second view controller and make the NewViewController it's delegate --
So create a second view controller with a delegate property.
something like this
#protocol SecondViewControllerDelegate;
#interface SecondViewController : UIViewController
#property (nonatomic, strong) NSString *currentString;
#property (nonatomic, assign) id<SecondViewControllerDelegate>delegate;
#end
//----------------------------------------------------------------------------------
#protocol SecondViewControllerDelegate <NSObject>
#optional
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string;
#end
Now in the implementation of the SecondViewController do something like this
#implementation SecondViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.currentString = [NSString stringWithFormat:#"Cell %ld", indexPath.row+1];
if ([self.delegate respondsToSelector:#selector(secondViewController:didChangeSelectionText:)]) {
[self.delegate secondViewController:self didChangeSelectionText:self.currentString];
}
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#end
Now in the implementation of the FirstViewController (your NewViewController) do something like this
#implementation FirstViewController
-(void) showSecondController
{
SecondViewController *viewController = [[SecondViewController alloc] init];
[viewController setDelegate:self]; // set this controller as the delegate
// add the rest of the display code here
}
#pragma mark - SecondViewControllerDelegate Methods
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string
{
//change your text here
}
#end
This should be enough to get you pointed in the right direction
In FirstViewController
- (void)changeTitleWithString:(NSString *)title :(UIView *)labelView
{
UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)];
[selected_station setFont:[UIFont systemFontOfSize:16.0]];
selected_station.textAlignment = NSTextAlignmentCenter;
selected_station.text = title;
[labelView addSubview:selected_station];
NSLog(#"%#", selected_station);
NSLog(#"%#", title);
}
InSecondViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.currentString = [NSString stringWithFormat:#"Cell %d", indexPath.row+1];
NewViewController *viewCOntroller = [[NewViewController alloc] init];
[viewController changeTitleWithString:self.currentString:self.view];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
I hope it helps
I have a tableView which I get the content from Parse.com (an image, one title and one description) and I am using a UIView for my detailView. When a cell is tapped UIView comes in with an animation. I managed to get My title and description of events but I couldn't get the Image file.
How to pass the parsed image into the UIView?
Thanks.
Here is my .h file
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "TableCell.h"
#interface ViewController : UIViewController <UITableViewDelegate> {
NSArray *events;
}
#property (weak, nonatomic) IBOutlet UITableView *newsTable;
#property (strong, nonatomic) IBOutlet UIView *detailView;
#property (strong, nonatomic) IBOutlet UILabel *InfoDetailLabel;
- (IBAction)backBtn:(id)sender;
#property (strong, nonatomic) IBOutlet UILabel *viewTitle;
Here is the .m file
#import "ViewController.h"
#import "TableCell.h"
#import "Reachability.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize newsTable;
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Pull to refresh
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.newsTable addSubview:refreshControl];
//Checking connection
if (![self connected])
{
// not connected
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Internet Connection Not Found" message:#"Please check your network settings!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
} else
{
// connected, do some internet stuff
}
// Do any additional setup after loading the view, typically from a nib.
[self performSelector: #selector(retreiveFromParse)];
}
//pull to refresh
- (void)refresh:(UIRefreshControl *)refreshControl {
[refreshControl endRefreshing];
}
- (void) retreiveFromParse {
PFQuery *retrieveEvents = [PFQuery queryWithClassName:#"News"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
events = [[NSArray alloc] initWithArray:objects];
}
[newsTable reloadData];
}];
}
//*********************Setup table of folder names ************************
//get number of sections in tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
//get number of rows by counting number of folders
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return events.count;
}
//setup cells in tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//setup cell
static NSString *CellIdentifier = #"EventCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFObject *tempObject = [events objectAtIndex:indexPath.row];
cell.TitleLabel.text = [tempObject objectForKey:#"Event"];
cell.DescriptionLabel.text = [tempObject objectForKey:#"Description"];
// To get the image file from Parse class
PFFile *imageFile = [tempObject objectForKey:#"imageFile"];
PFImageView *imageView = [[PFImageView alloc] init];
imageView.file = imageFile;
[imageView loadInBackground:^(UIImage *img,NSError *error){
if(!error)
{
UIImageView *yourImageView = [[UIImageView alloc] init];
yourImageView.image = imageView.image;
/*OR*/
cell.imageView.image = imageView.image;
}}];
return cell;
}
//user selects folder to add tag to
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cell tapped");
//For detail view
PFObject *tempObject = [ events objectAtIndex:indexPath.row];
NSLog(#"%#", tempObject.objectId);
_InfoDetailLabel.text = [tempObject objectForKey:#"detailinformation"];
[self animateDetailView];
_viewTitle.text = [tempObject objectForKey:#"Event"];
[self animateDetailView];
}
//for animation of detailview
- (void) animateDetailView {
[UIView animateWithDuration:0.3 animations:^{
_detailView.frame = CGRectMake(0, 0, 320, 518);
}];
}
//back button with animation
- (IBAction)backBtn:(id)sender {
[UIView animateWithDuration:0.3 animations:^{
_detailView.frame = CGRectMake(320, 0, 320, 518);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
and my cell .h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface TableCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
#property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
#property (strong, nonatomic) IBOutlet PFImageView *imageView;
#end
Try,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"cell tapped");
//Assuming not reordering the cells after it loaded
TableCell *cell = (TableCell *)[tableView cellForRowAtIndexPath:indexPath];
[[yourDetailView imageView] setImage:[[cell imageView] image]];
.....
}
or you can use the same method loadInBackground: used in cellForRow in detailView also
I need enlightenment with my view controllers. The first view controller has a UILabel and a UIButton. The UIButton leads to the second view controller.
The second view controller is a UITableView. Everything in this view is done programmatically including the UINavigationBar, UINavigationItem, and the UIBarButtonItem. It has NO UINavigationController.
So once a user selects a currency from the list, it should update the UILabel in the first view controller. But it won't.
first view controller .h:
#import <UIKit/UIKit.h>
#import "CurrencyListViewController.h"
#interface InformationViewController : UIViewController <CurrencyListViewControllerDelegate>
#property (nonatomic, retain) IBOutlet UILabel *currencyLabel;
- (IBAction)currencyButton;
#end
first view controller .m:
- (void)viewDidLoad
{
[super viewDidLoad];
[self createCurrencyButton];
[self createAcceptDeclineButton];
[self createCurrencyLabel];
}
- (IBAction)currencyButton
{
CurrencyListViewController *currencyView = [[CurrencyListViewController alloc] initWithNibName:#"CurrencyListViewController" bundle:nil];
currencyView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
currencyView.delegate = self;
[self presentViewController:currencyView animated:YES completion:nil];
}
- (void)createCurrencyLabel
{
currencyLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 120.0, 40.0)];
currencyLabel.hidden = NO;
currencyLabel.textColor = [UIColor whiteColor];
currencyLabel.textAlignment = NSTextAlignmentCenter;
[currencyLabel setEnabled:NO];
[self.view addSubview:currencyLabel];
}
- (void)currencySelected: (NSString *)currencySelected
{
currencyLabel.text = [NSString stringWithFormat:#"%#", currencySelected];
NSLog(#"Current currency is %#", currencyLabel.text);
}
second view controller .h:
#import <UIKit/UIKit.h>
#protocol CurrencyListViewControllerDelegate <NSObject>
- (void)currencySelected: (NSString *)currencySelected;
#end
#interface CurrencyListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
id<CurrencyListViewControllerDelegate> delegate;
}
#property (nonatomic, retain) UITableView *tableView;
#property (nonatomic, assign) id delegate;
#end
second view controller .m:
- (void)viewDidLoad
{
[super viewDidLoad];
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Currency"];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle: #"Cancel"
style:UIBarButtonItemStylePlain
target:self
action:#selector(cancelButtonPressed:)];
navigationItem.rightBarButtonItem = barItem;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, self.view.frame.size.height - navigationBar.frame.size.height) style:UITableViewStylePlain];
self.tableView = tableView;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:tableView];
[self.view addSubview:navigationBar];
[self showCurrencies];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [arrayOfCurrencies objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InformationViewController *informationViewController = [[InformationViewController alloc] init];
if ([_delegate respondsToSelector:#selector(currencySelected:)]) {
informationViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[_delegate currencySelected:[arrayOfCurrencies objectAtIndex:indexPath.row]];
NSLog(#"The user selects %#", [arrayOfCurrencies objectAtIndex:indexPath.row]);
}
[self presentViewController:informationViewController animated:YES completion:nil];
}
I am able to output:
2013-06-05 00:16:58.731 Testing[7056:c07] Current currency is AOA: Angolan Kwanza
2013-06-04 19:08:27.222 Testing[3613:c07] 2013-06-05 00:16:58.732 Testing[7056:c07] The user selects AOA: Angolan Kwanza
But the UILabel in my first view controller will NOT.
Why are presenting informationViewController again? dismiss the Current ViewController, which is CurrencyListViewController and do the currencySelected function
Should look like this:
[self dismissModalViewControllerAnimated:YES];
[_delegate currencySelected:[arrayOfCurrencies objectAtIndex:indexPath.row]];
I create one program that show list of book from url in tableview (any book has many images)
I want when to click any cell to go in next page (next page is UIScroll that show images) and show images of that book
I have one problem that it is when to click any cell and when go next page show black screen instead UIScrollView include many images.
this is my code :
RecipeViewController.h
#import "RecipeViewController.h"
#interface RecipeViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
#property (nonatomic,strong) IBOutlet UITableView *table;
#end
RecipeViewController.m
#import "RecipeViewController.h"
#import "Recipe.h"
#import "DetailViewController.h"
#implementation RecipeViewController
{
NSMutableArray *recipes;
NSInteger num;
}
#synthesize table;
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Recipe Book";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
NSString *numberbook = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:#"http://192.168.1.100/mamal/book.php?all"]];
NSInteger numbook = [numberbook integerValue];
for (int i = 1; i <= numbook; i++)
{
Recipe *si = [Recipe new];
//NSLog(#"%d,%#",i,si);
NSString *c = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.1.100/mamal/book.php?info=1&b=%d",i]]];
NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.1.100/mamal/book.php?p=1&b=%d",i]]];
si.name = [NSString stringWithString:c];
si.imageFile = data;
if(!recipes){
recipes = [NSMutableArray array];
}
[recipes addObject:si];
}
num = numbook;
// Remove table cell separator
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
// Assign our own backgroud for the view
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"common_bg"]];
self.tableView.backgroundColor = [UIColor clearColor];
// Add padding to the top of the table view
UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
self.tableView.contentInset = inset;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return recipes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageWithData:recipe.imageFile];
UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *obj = [[DetailViewController alloc]init];
int x = (indexPath.row)+1;
NSLog(#"x : %d",x);
obj.yourValue = [NSString stringWithFormat:#"%d",(indexPath.row)+1];
NSLog(#"yourValue1 : %#",obj.yourValue);
obj.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:obj animated:YES];
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#import "Recipe.h"
#import "RecipeViewController.h"
#interface DetailViewController : UIViewController<UIScrollViewDelegate>
{
UIScrollView *scroller;
}
#property (nonatomic,strong) IBOutlet UIScrollView *scroller;
#property (nonatomic,strong) Recipe *rec;
#property (nonatomic,strong) NSString *yourValue;
#end
DetailViewController.m
#import "DetailViewController.h"
#implementation DetailViewController
#synthesize scroller,yourValue;
- (void)viewDidLoad
{
[super viewDidLoad];
int m1 = [self.yourValue integerValue];
NSLog(#"M1 : %d",m1);
NSString *bookID = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://192.168.1.100/mamal/book.php?info=0&b=%d",m1]]];
NSInteger num1 = [bookID integerValue];
NSLog(#"%d",num1);
for (int i = 1; i <= num1; i++)
{
NSString *s = [NSString stringWithFormat:#"http://192.168.1.100/mamal/book.php?p=%d&b=%d",i,m1];
NSData *dat = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:s]];
NSLog(#"%#",s);
UIImageView *imagen = [[UIImageView alloc] initWithImage:[UIImage imageWithData:dat]];
imagen.frame = CGRectMake((i-1)*320, 0, 320, 460);
[scroller addSubview:imagen];
}
scroller.delegate = self;
scroller.contentSize = CGSizeMake(320*num1, 460);
scroller.pagingEnabled = YES;
}
#end
I have just solved this problem, if you are using StoryBoard you have to init the view controller this way:
UIStoryboard *storybrd = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
storybrd = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
YourViewController *newVC =[storybrd instantiateViewControllerWithIdentifier:#"YourVCId"];
You have to put your identifier in the storyboard, selecting the view you want to show and then selecting the identity inspector, there you have the option to write the id that you want.
You need to call:
[[DetailViewController alloc]initWithNibName:#"nibName" bundle:nil]; rather than just [[DetailViewController alloc]init]; as at this point, it doesn't have an Xib to inflate (unless you overrode the init method)
You not at all calling DetailViewController How it shows the image .Try this in didSelectRowAtIndexPath: may help you
DetailViewController *DVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:DVC animated:YES];