I have tried every thing I could do to solve the problem because I am newbie I am not getting the clear answer.
This is my first View controller
#implementation ViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"textAdd"]) {
TextAddViewController *tv=[[TextAddViewController alloc]init];
if ([self.text isEqualToString:#" "]) {
tv.usingText.text=#" ";
}
else{
tv.usingText.text=self.text;
}
}
}
-(IBAction)GetttingText:(UIStoryboardSegue*)segue
{
TextAddViewController *getText=segue.sourceViewController;
[self AddTextToCanvasWithGesture:getText.usingText];
}
-(void) LaunchText
{
[self performSegueWithIdentifier:#"textAdd" sender:self];
}
-(void)AddTextToCanvasWithGesture:(UITextView*)takenText
{
CGRect rect = CGRectMake(35, 60,takenText.contentSize.width,takenText.contentSize.height);
UIView *holderView = [[UIView alloc] initWithFrame:rect];
rect.origin.x=0;
rect.origin.y=0;
UILabel *textLabel=[[UILabel alloc]initWithFrame:rect];
[textLabel setText:takenText.text];
self.text=takenText.text;
[textLabel setTextColor:takenText.textColor];
[textLabel setFont:[UIFont fontWithName:takenText.font.fontName size:takenText.font.pointSize]];
[textLabel setNumberOfLines:0];
[textLabel sizeToFit];
[holderView setTag:1];
[textLabel setTag:1+1];
[holderView addSubview:textLabel];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
pinchRecognizer = nil;
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
rotationRecognizer = nil;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
panRecognizer = nil;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
tapRecognizer = nil;
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(LaunchText)];
[doubleTapRecognizer setNumberOfTapsRequired:2];
[doubleTapRecognizer setDelegate:self];
[holderView addGestureRecognizer:doubleTapRecognizer];
holderView.userInteractionEnabled=YES;
[tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
}
And This Is my Secondview contoller
#import "TextAddViewController.h"
#import "ViewController.h"
#interface TextAddViewController ()
{
}
#property (weak, nonatomic) IBOutlet UILabel *noting;
#end
#implementation TextAddViewController
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *vm=[[ViewController alloc]init ];
// [self.noting setText:#"Hi"];
[self.noting setText:vm.ShowText.text];
[self.usingText setText:vm.text];
}
My code runs perfect for displaying text of textview on Label but i want to edit text of label again so I have to display the text of label in same textview. Problem is it is not showing text in textview. every time textview is empty.
You are doing a couple of things wrong. The main thing is that you instantiate a new instance of your TextAddViewController. If you are new to object oriented programming this might be a bit difficult to understand (I suggest following this link (youtube) and learning a bit more before continuing on with xcode).
You correctly call the prepareForSegue method, which will fetch the storyboard segue you have created, and tag any data along that seg to your destinationViewController. Your problem is that you don't actually fetch your destinationViewController. Instead you are creating a new instance of TextAddViewController that will just float around in memory doing nothing.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"textAdd"]) {
TextAddViewController *tv=[[TextAddViewController alloc]init];
if ([self.text isEqualToString:#" "]) {
tv.usingText.text=#" ";
}
else{
tv.usingText.text=self.text;
}
}
}
so instead of this
TextAddViewController *tv=[[TextAddViewController alloc]init];
if ([self.text isEqualToString:#" "]) {
tv.usingText.text=#" ";
}
you should be doing this:
TextAddViewController *vc = [segue destinationViewController];
[vc setMyObjectHere:object];
Where in your case the object would be the string you want to set.
This way, you don't need you GetttingText IBAction.
I hope this helps you on your way. If you need some clarification feel free to ask. Another suggestion is to keep reading the Apple Documentation. It might not make sense at the moment, but eventually you will be able to find out stuff like this from the documentation
Related
I added tapGesture for self.view and UILabel (subview of mainView), each performs different selectors.
But the only main view tapGesture is being called and label tapgesture is not being called. How it is handled?
Here is the code:
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[lbl addGestureRecognizer:tapLblClick];
For two selectors only one method is called tapGesForSelf.
Here lbl is the subview of self.view.
Try this
- (void)viewDidLoad {
[super viewDidLoad];
[_lbl setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGesForSelf = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForSelf:)];
[self.view addGestureRecognizer:tapGesForSelf];
UITapGestureRecognizer *tapLblClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesForLbl:)];
[_lbl addGestureRecognizer:tapLblClick];
[tapGesForSelf requireGestureRecognizerToFail:tapLblClick];
}
- (void)tapGesForSelf:(UITapGestureRecognizer *)gesture
{
NSLog(#"self");
}
- (void)tapGesForLbl:(UITapGestureRecognizer *)gesture
{
NSLog(#"label");
}
I post answer for your question now once I tried and it worked well.
First see the label in design.I set label text as "Tap Me"
Now I set the code for view and label
- (void)viewDidLoad
{
[super viewDidLoad];
//TapGesture for Label
UITapGestureRecognizer *tapLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapLabel:)];
tapLabel.delegate = self;
tapLabel.numberOfTapsRequired = 1;
lblTapMe.userInteractionEnabled = YES;
[lblTapMe addGestureRecognizer:tapLabel];
//TapGesture for View
UITapGestureRecognizer *tapMainView = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(actionTapMainView:)];
tapMainView.delegate = self;
tapMainView.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tapMainView];
}
//Action method for Label
-(void)actionTapLabel:(UITapGestureRecognizer *)gestureOnLabel{
UILabel *label = (UILabel *)gestureOnLabel.view;
NSLog(#"Lable text is - %#",label.text);
}
//Action method for View
-(void)actionTapMainView:(UITapGestureRecognizer *)gestureOnMainView{
NSLog(#"The Main view is tapped");
}
Output Screenshot
Please setUserInteractionEnabled:YES for interact the TapGesture
[lbl setUserInteractionEnabled:YES];
I have a UIViewController where I'm adding bunch of UIViews as subviews. Then I attach UIGestureRecognizer to each, so I can be notified when there's a tap. Here's the code inside the UIViewController:
- (void)attachSubview{
UIImageView *childView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"child"]] ;
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[childView addGestureRecognizer:singleFingerTap];
[self.view addSubView childView];
}
- (void)tapped:(UITapGestureRecognizer *)recognizer{
NSLog(#"Tapped view : %#", recognizer.view);
NSLog(#"The root view is : %#", self.view);
}
Now, here's the problem: I actually want to extract these two methods out into a separate class. In this case I can't use the self.view inside the tapped: method, since self wouldn't be the viewcontroller class anymore.
So I would like a simple and efficient way to just take the recognizer object and somehow get the root view to which the image view belongs. What is the best and future proof way to do this?
Could you try this please?
- (void)viewDidLoad {
[super viewDidLoad];
//example of its use. You can replace with [className attachSubview:self.view] for example
self.view = [self attachSubview:self.view];
}
- (UIView *)attachSubview:(UIView *)passedInView{
UIImageView *childView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"child"]] ;
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[childView addGestureRecognizer:singleFingerTap];
[passedInView addSubview:passedInView];
return passedInView;
}
- (void)tapped:(UITapGestureRecognizer *)recognizer{
NSLog(#"Tapped view : %#", recognizer.view);
NSLog(#"The root view is : %#", recognizer.view.superview);
}
So , i have this void where i initialize the GUI and remove it when it's clicked the play image i call it with [self GUI:1]; and [self GUI:0];
The GUI comes up but when i try to hide it it's not working but it's entering in the if()
-(void)GUI:(int)action{
// GUY LOADING
UIImageView *menu =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-350/2,self.view.center.y-300/2,350,200)];
UIImageView *menuplay =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-270/2,self.view.center.y-50/2,133,50)];
UIImageView *menuretry =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x+5,self.view.center.y-50/2,133,50)];
menu.image=[UIImage imageNamed:#"menustart.png"];
menuplay.image=[UIImage imageNamed:#"menuplay.png"];
menuretry.image=[UIImage imageNamed:#"retrymenu.png"];
if(action == 1){
[self.view addSubview:menu];
[self.view addSubview:menuplay];
[self.view addSubview:menuretry];
}
if(action == 0){
[menu removeFromSuperview];
[menuplay removeFromSuperview];
[menuretry removeFromSuperview];
}
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(StartGame:)];
singleTap.numberOfTapsRequired = 1;
[menuplay setUserInteractionEnabled:YES];
[menuplay addGestureRecognizer:singleTap];
}
the StartGame selector only executes the following code
[self GUI:0];
and the viewDidLoad only executes the following code
[self GUI:1];
Every time the methode GUI: is called, no it not called a void you create new instance of the imageviews. Also GUI is not a good name for a method, better would be something like: setMenuVisible:
Since there is no reference to the old, previous image view they can not removed, You need to keep a reference to the image views.
So in your header do the following:
#property (strong, nonatomic) UIImageView *menu;
#property (strong, nonatomic) UIImageView *menuplay;
#property (strong, nonatomic) UIImageView *menuretry;
Then in you GUI: methode:
-(void)GUI:(int)action{
// GUY LOADING
if (!self.menu) {
self.menu =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-350/2,self.view.center.y-300/2,350,200)];
menu.image=[UIImage imageNamed:#"menustart"];
}
if (!self.menuplay) {
self.menuplay =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x-270/2,self.view.center.y-50/2,133,50)];
menuplay.image=[UIImage imageNamed:#"menuplay"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(StartGame:)];
singleTap.numberOfTapsRequired = 1;
[menuplay setUserInteractionEnabled:YES];
[menuplay addGestureRecognizer:singleTap];
}
if (!self.menuretry) {
self.menuretry =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.center.x+5,self.view.center.y-50/2,133,50)];
menuretry.image=[UIImage imageNamed:#"retrymenu"];
}
if(action == 1){
[self.view addSubview:self.menu];
[self.view addSubview:self.menuplay];
[self.view addSubview:self.menuretry];
}
else if(action == 0){
[self.menu removeFromSuperview];
[self.menuplay removeFromSuperview];
[self.menuretry removeFromSuperview];
}
}
I have tried every thing I could do to solve the problem because I am newbie I am not getting the clear answer.
I have two view controllers. I passed the data of textview to 2nd controller and displaying it in UILabel and there is no problem with it. But now I have to send the the text of label again to the same textview for editing. The problem is when I double tap on label it navigates me to the 1st View but does not displaying the text in textview.
-(void)AddTextToCanvasWithGesture:(UITextView*)takenText
{
CGRect rect = CGRectMake(35, 60,takenText.contentSize.width,takenText.contentSize.height);
UIView *holderView = [[UIView alloc] initWithFrame:rect];
rect.origin.x=0;
rect.origin.y=0;
UILabel *textLabel=[[UILabel alloc]initWithFrame:rect];
[textLabel setText:takenText.text];
self.text=takenText.text;
[textLabel setTextColor:takenText.textColor];
[textLabel setFont:[UIFont fontWithName:takenText.font.fontName size:takenText.font.pointSize]];
[textLabel setNumberOfLines:0];
[textLabel sizeToFit];
[holderView setTag:1];
[textLabel setTag:1+1];
[holderView addSubview:textLabel];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
tapRecognizer = nil;
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(LaunchText)];
[doubleTapRecognizer setNumberOfTapsRequired:2];
[doubleTapRecognizer setDelegate:self];
[holderView addGestureRecognizer:doubleTapRecognizer];
holderView.userInteractionEnabled=YES;
[tapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
}
I have to send this label's text to textview of 2ndview controller
-(void) LaunchText{
[self update:self.text];
[self performSegueWithIdentifier:#"textAdd" sender:self];
}
-(void)update:(NSString*)string{
TextAddViewController *txt=[[TextAddViewController alloc]init];
txt.usingText.text=string;
}
and i am fetching this string in 2nd view as
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *vm=[[ViewController alloc]init];
self.usingText.text=vm.text;
}
Override below method in your SecondViewController, And pass UILabel values to FirstViewController.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"prepareForSegue is called");
if ([[segue identifier] isEqualToString:#"CONTROLLER_SEGUE"]) {
TextAddViewController *vc = [segue destinationViewController];
[vc.usgingText setText:#"SOME TEXT"];
}
}
ViewController containing the textView needs a line of code in its view did Load method.
textView . text = where ever the text came from.
Posting more codes will help you better.
I know that a "table view header"(the most top-part of a table view) is a View
So I try to add a UITapGestureRecognizer to it ,but it doesn't work...
code is simple :
- (void)tap:(UITapGestureRecognizer *)recognizer
{
// do something
}
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
Any tips here to care ? thanks a lot
Here's the thing that works for me:
Instead adding this:
self.tableView.tableHeaderView
I add gesture recognizer on every UILabel on tableview.
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [UIColor greenColor];
headerLabel.text = [NSString stringWithFormat:#"Header No.%d",section];
headerLabel.frame = CGRectMake(0, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(catchHeaderGesture:)];
tapGesture.cancelsTouchesInView = NO;
[headerLabel addGestureRecognizer:tapGesture];
return headerLabel;
//return nil;
}
-(void)catchHeaderGesture:(UIGestureRecognizer*)sender
{
UILabel *caughtLabel = (UILabel*)sender.view;
NSLog(#"header no : %d", caughtLabel.tag);
}
I hope that helps.
First of all
Make sure that you call this code section in viewDidLoad or viewWillAppear
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];
Second, please make sure that
self.tableView.tableHeaderView
is not null, add
NSLog([self.tableView.tableHeaderView description]);
And check the console for output
I just tried your code and the tap was recieved correctly