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]];
Related
I am trying to send data back from one view controller to another. I am doing this based off another Q & A in stack overflow but I still have not go the hang of it.
LocationViewController.h
#import "ViewController.h"
#class LocationViewController;
#protocol LocationViewControllerDelegate <NSObject>
- (void)addItemViewController:(LocationViewController *)controller didFinishEnteringItem:(NSString *)item;
#end
#interface LocationViewController : UIViewController
#property (nonatomic, strong) UILabel *locationLabel;
#property (nonatomic, strong) UITextField *locationField;
#property (nonatomic, strong) NSString *fieldText;
#property (nonatomic, weak) id <LocationViewControllerDelegate> delegate;
- (instancetype) init;
-(void)saveButtonPressed;
#end
LocationViewController.m
#import "LocationViewController.h"
#import "SetScoringTableViewController.h"
#import "GameDetailsTableViewController.h"
#interface LocationViewController ()
#end
#implementation LocationViewController
#synthesize locationField;
#synthesize locationLabel;
#synthesize fieldText;
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
//label
locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 1000, 20)];
[locationLabel setText: #"Location:"];
[self.view addSubview:locationLabel];
//text field
locationField = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, 300, 35)];
[locationField setBorderStyle: UITextBorderStyleRoundedRect];
locationField.text = #"Enter Location Here";
[self.view addSubview:locationField];
//save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle: #"Save" style: UIBarButtonItemStylePlain target: self action: selector(saveButtonPressed)];
self.navigationItem.rightBarButtonItem = saveButton;
}
-(void)saveButtonPressed {
locationField.text = locationField.text;
[self.delegate addItemViewController:self didFinishEnteringItem:locationField.text];
NSLog(#"%#", locationField.text);
[self.navigationController popViewControllerAnimated:YES ];
[self performSelector:#selector(saveButtonPressed) withObject:nil afterDelay:0.25];
GameDetailsTabelViewController *gameDetails = [[GameDetailsTableViewController alloc] init];
[gameDetails.tableView reloadData]
}
GameDetailsTableViewController.m:
-(void)addItemViewController:(LocationViewController *)controller didFinishEnteringItem:(NSString *)item {
LocationViewController *locationView = [[LocationViewController alloc]initWithNibName:#"LocationViewController" bundle:nil];
locationView.delegate = self;
[[self navigationController]pushViewController:locationView animated:YES];
item = fieldText;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifer1 = #"GameDetailsLocationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer1];
label = (UILabel *)[cell viewWithTag:0];
label.text = [NSString stringWithFormat: #"%#", fieldText];
return cell;
}
}
When I run this I get null for the item. I would like to know why I am getting null on the item which is preventing my from further usage of this program.
I am guessing that you wanna send some info from LocationVC to GameDetailVC.
In that case
LocationViewController.m
-(void)saveButtonPressed {
if([self.delegate respondsToSelector:#selector(addItemViewController:didFinishEnteringItem:)]{
[self.delegate addItemViewController:self didFinishEnteringItem:locationField.text];
}
[self.navigationController popViewControllerAnimated:YES ];
}
GameDetailsTableViewController.m:
-(void)addItemViewController:(LocationViewController *)controller didFinishEnteringItem:(NSString *)item {
//do something with item?
//reload table data?
[self.tableview reloadData];
}
Hope it helps.
check for errors.
First thing to note is that I don't use xibs or storyboards, so everything needs to be done programatically.
I have a UITableView and I need too add a custom static view to the bottom of the screen when the table view is open (it'll function as a toolbar). I can't add it as a direct subview of the tableview because then it scrolls with the table. Also worth mentioning is that all of this is sitting inside of a UINavigationController.
Anyone know how I can get around this?
You can add UIToolbar to the bottom of the screen with UIBarButtonItems and add UITableView to the top of the view controller with height equal tables view height - toolbar height.
Here is a implementation subclassing UIViewController and adding UITableView and UITabBarController to it.
#interface RKViewController ()<UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) UITableView *tableView;
#property (nonatomic, strong) UITabBar *tabBar;
#property (nonatomic, strong) UITabBarController *tabBarController;
#end
#implementation RKViewController
-(id)init
{
self = [super init];
if (self) {
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 500)];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tabBarController = [[UITabBarController alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = #"Item 1";
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.tabBarItem.title = #"Item 2";
self.tabBarController.viewControllers = [NSArray arrayWithObjects:vc1,vc2, nil];
[self.view addSubview:self.tableView];
[self.view addSubview:self.tabBarController.view];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UITableViewCell new];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
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 am trying to show a UIViewController on top of other UIViewcontroller using addChildViewController functionality.The childViewController is a tableView which shows up on top of my MainViewController however I do not see the table view that it has.If I execute the childViewController separately , the tableView works fine , so what am I missing here.
Here's how I am adding a childVC:
#implementation Test2ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)showChildVC:(id)sender
{
TestTableViewController *tVC = [[TestTableViewController alloc]init];
tVC.view.frame = CGRectMake(50, 50, 200, 200);
[self addChildViewController:tVC];
[self.view addSubview:tVC.view];
[tVC didMoveToParentViewController:self];
}
And this is the childVC that I want to show: .h
#import <UIKit/UIKit.h>
#interface TestTableViewController : UIViewController<UITableViewDataSource>
{
NSArray *array;
}
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
And: .m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
array = [NSArray arrayWithObjects:#"One",#"Two",#"Three",#"Four", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array 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];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
I see your table view in the second view controller is an IBOutlet, so you are placing it in Storyboard.
Then when you instantiate it, you can't do: [[TestTableViewController alloc]init]; you have to do:
[storyBoard instantiateViewControllerWithIdentifier:#"tVCStoryBoardID"];
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];