UILabel to NSString to load webview - nsstring

So I have this code, where I am sending a variable storyLink to a label and display it in the specified nib. So far the label displays the text which is what I want. But I want to use that text as an url for a webview. urlString = nothing as of right now because I can't figure out how to use/convert an UILabel to a String suitable for a webview url. So if that was confusing basically I want to take the text stored in my label and use it for the url in my webview. Can someone help?
TestView.h
#interface TestView : UIViewController {
IBOutlet UILabel *label;
IBOutlet UIWebView *webView;
NSString *urlString;
}
#property(nonatomic, retain)IBOutlet UILabel *label;
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#property(nonatomic, retain)NSString *urlString;
#end
TestView.m
- (void)viewDidLoad {
[super viewDidLoad];
label.text=nil;
urlString=something;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
sending code
TestView *objFirstController=[[[TestView alloc] initWithNibName:#"TestView" bundle:nil] autorelease];
[self.navigationController pushViewController:objFirstController animated:YES];
objFirstController.label.text=storyLink;

When setting properties that are accessed during 'viewDidLoad', make sure to put them before you use pushViewController.
TestView.m
- (void)viewDidLoad
{
[super viewDidLoad];
label.text=urlString;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
sending code
TestView *objFirstController = [[[TestView alloc] initWithNibName:#"TestView" bundle:nil] autorelease];
objFirstController.urlString = storyLink;
[self.navigationController pushViewController:objFirstController animated:YES];

Related

How to set title for Navigation Bar

How to set the title on a root viewcontroller inside a navigation viewcontroller. Generically, the answer seems to be, "set self.title of some viewcontroller". but this only works for the viewcontrollers I am pushing to. On the initial viewcontroller, running self.title = #"my title"; or self.navigationController.navigationItem.title = #"my titles"; don't work.
Thinking that is needed to be set earlier on, I did this in app delegate:
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] initWithNibName:#"MainMenuViewController" bundle:nil];
mainMenuViewController.title = #"coding chal";
self.navController = [[UINavigationController alloc] initWithRootViewController:mainMenuViewController];
What must I do for the root viewcontroller to have a title?Thanks in advance
Setting the view controller's title does work. When you run into trouble, the best course is make yourself a minimal test example. Here's one. This is the only code in the app!
In the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [UIWindow new];
ViewController* vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
In the view controller:
- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self.title = #"Testing";
return self;
}
Result:
So you can see that it really does work. If it seems not to work in your app project, therefore, that must be because of other code somewhere.

Passing data back from detailViewController to UITableView

I am currently at the issue where I have a UITableviewCell that needs to be updated.
When the user presses on the uitableviewcell - THERES ONLY 1!!, the user is pushed to a UITABLEVIEWCONTROLLER where the user is allowed to select 1 of multiple cells with their own titles.
I need to get the clicked tableviewcells title and pass the value back to the parentviewcontroller and update the 1 tableviewcell's name to the one the user clicked in the pushed uitableivewcontroller.
Here is a picture of the parent viewcontroller...
And heres the picture of the pushed viewcontroller....
I was told earlier yesterday that delegation would be needed but I am unsure what to do at this point :/.
Heres some code I use in the parent viewcontroller...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
ProblemTypes *view = [[ProblemTypes alloc] init];
[self.navigationController pushViewController:view animated:YES];
}
I am also NOT USING storyboards, just a few xibs.
Also heres the code for the pushedviewcontroller to pop to the parent viewcontroller when a cell is selected...
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"Cell's text: %#",cell.textLabel.text);
[self.navigationController popViewControllerAnimated:YES];
}
Thank you guys!
Found it out.... Do delegation was the solution... Just hope it's the MOST EFFICIENT!!! Here's the code for delegation.
First, implement the delegate of the parentViewcontroller and it's methods, also make sure to add delegation to the parentviewcontroller...
#protocol SendFeedBackDelegate
- (void) didReceiveType:(NSString *) message;
#end
#interface SendFeedBackViewController : UIViewController <SKPSMTPMessageDelegate, UITableViewDataSource,UITableViewDelegate, SendFeedBackDelegate>
{
NSString *subject;
}
Next, implement the method : - (void) didReceiveType:(NSString *) message; under
#implementation SendFeedBackViewController
- (void) didReceiveType:(NSString *) message
{
subject = message;
[feedbackTableView reloadData];
// I reload the data because it is needed when this function is going to be called
// in the child viewcontroller.... just keep reading :)
}
Now go to - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath for the use of this example and my project :)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [feedbackTableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// THIS IS THE IMPORTANT PIECE OF CODE YOU NEED TO NOTICE.....
// it allows for the first thing the tableview cell to be is a static string until subject
// it is changed and the user chooses a subject in the childviewcontroller
if (subject == nil) {
cell.textLabel.text = #"Select a Product";
} else {
cell.textLabel.text = subject;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Now, add a protocol to the childviewcontroller to allow the childviewcontroller to conform to the parentviewcontroller
In childviewcontroller.h: add these lines of code,
#import "ParentViewController.h"
#protocol SendFeedBackDelegate;
#interface FeedbackTypes : UITableViewController
{
id<SendFeedBackDelegate> delegate;
}
#property (nonatomic, assign) id<SendFeedBackDelegate> delegate;
Now you have set the delegate in the parent viewcontroller.... Next head over the same files implementation file (.m) and add these:
//Add synthesize just under #implementation "ClassName"
#synthesize delegate;
// I used a uitableviewcontroller for this example so refer to the problem I have above
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//NSLog(#"Cell's text: %#",cell.textLabel.text);
[delegate didReceiveType:cell.textLabel.text];
[self.navigationController popViewControllerAnimated:YES];
}
And THATS IT!!!!..... :), Hope this was a simple and basic tutorial and here's a snapshot.

iOS: Pointer get lost when poping back to parentView

I code for iOS 6.1 with ARC and have a problem that my objects in the nsmutablearray get "lost".
I will explain it in more detail:
There is a parent view which has a NSMutableArray which should hold some objects(addresses)
Everytime I hit the button the data of the Array will be displayed with NSLog and then the navigation controller pushes to the ChildView
The childview generates a new address object
If I do this and then press the back button in the view and wants to try the same case (again pressing the button) the data in the array is lost and I get a EXC_BAD_ACCESS
My assumption: When I go back to parentView ARC deallocates the second view and everything in it. But my Address object will be referenced from the array so that the ARC counter for this object should not be 0.
Can anyone help me?
Here is the concrete code
ParentViews Controller h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) NSMutableArray *adresses;
#property int count;
-(void)goFurther;
#end
ParentViews Controller m:
#import "ViewController.h"
#import "SecondViewController.h"
#import "Address.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
self.title=#"First View";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Add address" style:UIBarButtonItemStylePlain
target:self action:#selector(goFurther)];
[self.navigationItem setLeftBarButtonItem:addButton];
self.adresses=[[NSMutableArray alloc] init];
self.count=0;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) goFurther{
for (Address *a in self.adresses) {
NSLog(#"Adresse:");
NSLog(a.street);
NSLog(a.door);
}
self.count++;
SecondViewController *second=[[SecondViewController alloc]initWithView:self];
[self.navigationController pushViewController:second animated:true];
}
#end
ChildViews Controller h:
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface SecondViewController : UIViewController
#property ViewController * viewCon;
-(id) initWithView: (ViewController*) view;
#end
ChildViews Controller m:
#import "SecondViewController.h"
#import "Address.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(id) initWithView: (ViewController*) view{
self = [super init];
self.viewCon=view;
Address *a=[Address alloc];
a.street=[NSString stringWithFormat:#"Street %d", self.viewCon.count];
a.door=#"Door";
[self.viewCon.adresses addObject: a];
return self;
}
- (void)viewDidLoad
{
self.title=#"Second View";
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You have to send your data back to the parent view before dismissing the child view.
Declare a #protocol (say, called ChildControllerDelegate) in your child controller's .h file with a method didDismissWithData: with some data type that is suitable for you (maybe string or dictionary). Have the parent view controller conform to the protocol. Create a #property of the child called delegate of type id<ChildControllerDelegate>
Then before dismissing the child, call
[self.delegate didDismissWithData:newData];
In the parent controller, handle this message and save your new address.

loading another tableview when a tablecell of one tableview is clicked returning error:"Program received signal SIGABRT"

I am creating an app in which there are two UIViews and in those UIViews I am loading Tableviews..
When I click a tablecell in one TableView then I am unable to redirect it to another TableView and getting error:Program received signal SIGABRT.But if I want to load a UIView when a tablecell is clicked it gets executed perfectly.I couldn't understand where Am I going wrong....
This is the code i'm writing
ViewController1:
#import ViewController2.h"
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ViewController2 *v2 = [ViewController alloc] initWithNibName:#"ViewController2" bundle:[NSBundle mainBundle]];
[self presentModalViewController:v2 animated:NO]; **//getting error at this line**
[v2 release];
}
ViewController2.h
#import"ViewController1.h"
- (void)viewDidLoad
{
[super viewDidLoad];
tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(10, 10, 320, 460)];
tableView1.delegate = self;
tableView1.dataSource = self;
[self.view addSubview:tableView1];
}
Couldn't understand what could be the possible cause of this error..
Maybe this is the bug
ViewController2 *v2 = [ViewController alloc] initWithNibName:#"ViewController2" bundle:[NSBundle mainBundle]];
You have allocated ViewController instead of ViewController2
Try this and it should work, I guess.
ViewController2 *v2 = [ViewController2 alloc] initWithNibName:#"ViewController2" bundle:[NSBundle mainBundle]];

How to log the backbutton that comes with the navigationcontroller

I am creating a new view using
[[self navigationController]pushViewController:newViewController animated:YES];
The new view (newViewController) stacks on the top of the previous, and comes with a backbutton. I need to access this backbutton to override it. To begin with, finding out how to NSLog a string when the back button is pushed will be of great help.
Best, Tom
I found the answer myself.
I have to make a new backbutton in the viewDidLoad function, and add a selector.
-(void) viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"myTitle"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(mySelector:)];
self.navigationItem.leftBarButtonItem = backButton;
}
Then the selector:
-(void)mySelector
{
NSLog(#"backButton pushed");
}

Resources