navigation between views in iOS [closed] - ios

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im trying to make my first app in Xcode. This tutorial http://www.youtube.com/watch?v=rgd6mCuzlEc helped me understand how to make navigation between views. In my app I need more buttons on first and second view but buttons on second view are dependet on buttons from first view.
For example buttons on first view:
- birds
- dogs
- fishes
second view for birds:
- stork
- sparrow
- chicken
On a third view will be describtion for chosen animal.
What should be my next step? Example from link is good for my app?

You could declare your initializer for your second view controller to take data. When you initialize that view controller from your first view controller, just be sure to pass the data. Then you could set the button text in your viewDidLoad: method on your second view controller (You could try doing this in your initWithData method but sometimes xib elements aren't properly initialized at that point)

what you need to do is create a public (in the .h file) variable in the second view. lets say this variable will be NSString:
#property (nonatomic,strong) NSString *caseStr;
now dont forget to create an initialiser in your second view .m file
#synthesize caseStr = _caseStr;
-(void)setCaseStr:(NSString *)caseStr
{
_caseStr = caseStr;
}
now on your first view implement the "prepareForSegue" method:
(we assume that your segue identifier is "ChosenAnimal" and type of bird is a local string that set to be "Birds","Dogs" or "Fish"
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ChosenAnimal"]) {
[segue.destinationViewController setCaseStr:typeOfBird];
}
}
now you have the info of what been selected in the first view and you can do with it whatever you want in the second view.
BTW: if you would ever like to control the first view from the second view you will have to use delegate. good luck

Related

Why is this error happening with prepareForSegue in Swift? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I created a navigation controller and put 2 view controllers linked to it. One is called FirstLaunchVC and the other is FirstLaunchVC2, I want the user to put his name in the text field nameTxtField and when he clicks on the continueBtn it should lead to the other. In this second view (FirstLaunchVC2) there is a label called nameGreetings which will show the name of the user as a greeting with prepareForSegue, the thing is that it's crashing, saying that there are breakpoints on the line of the performSegueWithIdentifier and on the line where I write "nextVC.nameGreetings.text = "(str) etc etc". I have no clue why, can anyone help me with that? Btw, I've already checked the identifier and it's correct.
Is nameGreetings an IBOutlet? You cannot set the IBOutlet controls for the destination view controller in prepareForSegue of the originating view controller because while the destination view controller has been instantiated, its views and IBOutlet references have not. The prepareForSegue should feel free to update String properties of the destination, but not its IBOutlet references.
So updating of name is fine, but the nameGreetings should not be set in prepareForSegue, but rather that should be deferred until the the viewDidLoad of that FirstLaunchVC2.

How to label the row the way iPhone built in alarm app labeled its alarm name? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to label the row just like the way iPhone alarm app label's its alarm name. I don't want a back button. I just want to pass whatever text I input to be passed to the MainViewController.
I am using delegate, but I am not quite sure where to put my delegate code in my ViewController which has my texfield. I have 2 ViewControllers connected with "show" segue. One has table row and another has textfield.
Code below:
second.h (where textfield is)
#protocol LabelNameDelegate <NSObject>
- (void)LabelNameEntered:(NSString*)labelName;
#end
#property(nonatomic, weak) id<LabelNameDelegate> delegate;
second.m
[self.delegate LabelNameEntered:self.myLabel.text];(not sure where to place it exactly)
first.h
#interface FirstTableViewController : UITableViewController<LabelNameDelegate>
first.m
- (void)LabelNameEntered:(NSString*)labelName{
NSLog(#"This is text from secondview: %#", labelName);
}
If I wanted to accomplish this task, I'd create a delegate protocol in my textfield view controller's .h file, then create methods that my textfield view controller could call to let the delegate - which would be the table view's view controller - know what the user entered in the text field.
Edit: This page should help you figure out how to create your own delegate protocol: How do I create delegates in Objective-C?
Edit: If you tried this and can't get it to work, please post some code so we can help you.
In the second.m implement
- textFieldDidBeginEditing:
So whenever the textField is done editing, you can call the delegate you have
Note I am not sure why you have a label here, shouldn't it be textfield?
[self.delegate LabelNameEntered:self.myLabel.text];
I placed my delegate code in the following method, it worked perfectly. The following code means, once second viewController is closed, pass following value to the first viewController.
second.m
- (void)viewWillDisappear:(BOOL)animated {
[self.delegate LabelNameEntered:self.myLabel.text];
}

How to assign a set of methods to UITextFieldDelegate [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to know how to fire a method when a particular text box is edited.
For example, it is explained here how to assign a certain format to a textbox, using a number of functions. However, I don't know where to copy-paste these functions. In the explanation it says to use UITextFieldDelegate, but I don't know where it is.
I have a AppDelegate class, a ViewController class (including its xib), and a UITextField in the xib. I want the code to work for this UITextField.
Add those methods in your ViewController.m class
and then in your viewDidLoad method of ViewController.m Class
[yourTextField addTarget:yourTextFieldDelegate
action:#selector(reformatAsCardNumber:)
forControlEvents:UIControlEventEditingChanged];
set reformatCardNumber: to be called whenever the text field fires a UIControlEventEditingChanged event
also add delegete in Viewcontroller.h file
and Thats Done.

Changing UIView To be instance from UIControl Programmatically [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a button that will change the view's class to UIControl
i have typed this line of code but Xcode say it is a error
-(IBAction)button:(id)sender
{
self.view.class = [UIControl class];
}
So guys my question is how to change my view's class to UIControl class programmatically
In objective C you cannot assign the class property of an object and within instance of UIViewController (which I'm assuming you're code resides in) you cannot assign the view property outside of loadView without causing issues.
I think your general question on changing an objective C's class is slightly misguided and you may need reword your question such that it's possible to suggest a way to do what you're trying to do in cocoa.
What are you actually trying to do? Are you attempting to change what's displayed on screen in response to an event? Are you trying to change the behaviour of your view controller's view somehow?
These things are usually done by adding/changing/modifying the view hierarchy of your view controller by adding other UIView instances rather than modifying existing ones.
You can define your own class that inherits form UIControl easily but there's a lot more you need to do to begin using it in the above example.
#interface CustomClass : UIControl
…
#emd
Can I suggest the following introduction to Objective C and iPhone programming guides form Apple that may shed some light on how to do things in Cocoa.
Learning Objective C - A Primer
Programming with Objective C
iOS App Programming Guide
You cant change the class type at runtime.
Alternatively you can set your control to stop handling user interactions:
self.userInteractionEnabled = NO;

What is a delegate in iOS? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been flashing through some questions here about xcode iOS programming, and I've seen more and more people say something like "as the delegate of blabla" things like this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
currentLocationAnnotation = [annotation retain];
}
But I could never help me because I don't understand what this means, and where I have to put it, if I put it in my .m file, it doesn't work, and if I put it in my AppDelegate.m it doesn't work either.
Please help :)
In your code this is the custom delegate what it means it just work like a helper object in which whatever methods had implemented inside custom delegates can be used.
It's a design pattern where you register a object(the delegate), that confirms to a specified protocol, with another instance of some other class that then calls the first objects "protocol methods"(delegate methods) when it wants the delegate to perform some work. This is usually used to avoid unnecessary subclassing when a object just wants to "outsource" part of it's work.
It's a bit hard to get in the beginning. A delegate of a class is like the handler of events of that class. For example, for the map view class, you set a delegate (e.g. your custom class), and in your custom class, start implementing the handlers (it doesn't necessarily need to handle events. For example table view asks its delegate about how many rows it will have, their height etc.). For example, in the example code you've posted, the map view is asking its delegate to return the view for the annotation object that it's sending as a parameter. You need to implement your logic in your class to return the appropriate object.
In English terms, think of it as an email: 'Hello, my delegate, as a part of your job, I'd like you to give me the view for annotation that I'm attaching below, Thanks, Map View.'
Of course, it requires an immediate response from the delegate right now, and the 'attached below' is the 'annotation' parameter. Hope it helps.

Resources