Can'T click textView when delegate is set - ios

I have an App with 2 Views on top of each other and on the overlaying view there is a UITextView and a UIButton next to it.
When I click the Button once the TextView should become smaller (from CGRectMake(0, 50, self.view.frame.bounds.size.width, 100) to CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)) and on the second click it should become bigger again.
Here's the code how I manage this:
-(IBAction)buttonTouched:(id)sender {
if(counter == 1) {
[self.textView removeFromSuperview];
self.textView = nil;
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)];
[self.overlayView addSubview:self.textView];
counter = 2;
} else {
[self.textView removeFromSuperview];
self.textView = nil;
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 100)];
[self.overlayView addSubview:self.textView];
counter = 1;
}
}
My problem is that when I do it like this, I can't handle the delegate methods for the UITextView like textViewDidBeginEditing:(UITextView *)textView without setting the delegate. But when I set the delegate like self.textView.delegate = self after recreating the TextView, nothing happens when I click on it. No keyboard appears and the events aren't called either.
Any suggestions?

Try to alloc your textView in the ViewDidLoad,
self.textView = [[UITextView alloc]init];
and on the button click use
[self.textView setFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)];
and
[self.textView setFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 100)];

-(IBAction)buttonTouched:(id)sender {
if(counter == 1) {
[self.textView removeFromSuperview];
self.textView = nil;
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 50)];
[self.overlayView addSubview:self.textView];
counter = 2;
} else {
[self.textView removeFromSuperview];
self.textView = nil;
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.bounds.size.width, 100)];
[self.overlayView addSubview:self.textView];
counter = 1;
}
}
In above code only change "self.view.bounds.size.width" insted of "self.view.frame.bounds.size.width"
Assign delegate "self.textView.delegate=self" before that in .h file
#interface ViewController : UIViewController

Try this,
Initialise and addSubview self.textView in viewDidLoad and set self.textView.delegate = self;
-(IBAction)buttonTouched:(id)sender {
if(counter == 1) {
self.textView.frame = CGRectMake(0, 50, self.view.frame.bounds.size.width, 50);
counter = 2;
} else {
self.textView.frame = CGRectMake(0, 50, self.view.frame.bounds.size.width, 100);
counter = 1;
}
[self.overlayView bringSubviewToFront:self.textView];
}
Just change the frame of self.textView in buttonTouched:

Related

Scroll down and up in UIScrollVIew Objective C

I want to scroll up and down in my page without using AutoLayout in Xcode. How do I make the scrolling work?
I want to show the second question label and text and add other labels and fields. But I can't scroll to see how they look after building the project
This is the viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *bg = [UIImage imageNamed:#"logo2.jpg"];
self.myImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.myImageView.image = bg;
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
self.myImageView.center = self.view.center;
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.myImageView];
CGRect scrollViewRect = self.view.bounds;
self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
self.myScrollView.pagingEnabled = YES;
self.myScrollView.scrollEnabled = YES;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 100, self.myScrollView.frame.size.height + 100);
[self.view addSubview:self.myScrollView];
float width = CGRectGetWidth(self.myScrollView.frame);
float height = CGRectGetHeight(self.myScrollView.frame);
float newPosition = self.myScrollView.contentOffset.y+height;
CGRect toVisible = CGRectMake(newPosition, 0, width, height);
[self.myScrollView scrollRectToVisible:toVisible animated:YES];
//FirstNameLabel
CGRect labelFirst = CGRectMake(10, 100, 100, 30);
self.firstnamelabel = [[UILabel alloc] initWithFrame:labelFirst];
self.firstnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.firstnamelabel.text = #"First Name : ";
//FirstNameField
CGRect firsttextField = CGRectMake(100, 100, 200, 30);
self.firstnametext = [[UITextField alloc] initWithFrame:firsttextField];
self.firstnametext.borderStyle = UITextBorderStyleRoundedRect;
//LastNameLabel
CGRect labelLast = CGRectMake(10, 150, 100, 30);
self.lastnamelabel = [[UILabel alloc] initWithFrame:labelLast];
self.lastnamelabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.lastnamelabel.text = #"Last Name : ";
//LastNameTextField
CGRect lasttextField = CGRectMake(100, 150, 200, 30);
self.lastnametext = [[UITextField alloc] initWithFrame:lasttextField];
self.lastnametext.borderStyle = UITextBorderStyleRoundedRect;
//DateLabel
CGRect labeldate = CGRectMake(10, 190, 100, 30);
self.DateOfBirthLabel = [[UILabel alloc] initWithFrame:labeldate];
self.DateOfBirthLabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.DateOfBirthLabel.text = #"Date Of Birth: ";
//DatePicker
CGRect datepickersize=CGRectMake(10, 210, 300, 150);
self.datepicker=[[UIDatePicker alloc] initWithFrame:datepickersize];
NSDate *date= self.datepicker.date;
self.datepicker.datePickerMode=UIDatePickerModeDate;
//Email
CGRect labelemail = CGRectMake(30, 370, 100, 30);
self.emaillabel = [[UILabel alloc] initWithFrame:labelemail];
self.emaillabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.emaillabel.text = #"Email : ";
//EmailText
CGRect emailtextField = CGRectMake(100, 370, 200, 30);
self.emailtext = [[UITextField alloc] initWithFrame:emailtextField];
self.emailtext.borderStyle = UITextBorderStyleRoundedRect;
//PasswordLabel
CGRect labelpassword = CGRectMake(10, 410, 100, 30);
self.passwordlabel = [[UILabel alloc] initWithFrame:labelpassword];
self.passwordlabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.passwordlabel.text = #"Password : ";
//PasswordText
CGRect passwordtextField = CGRectMake(100, 410, 180, 30);
self.passwordtext = [[UITextField alloc] initWithFrame:passwordtextField];
self.passwordtext.borderStyle = UITextBorderStyleRoundedRect;
self.passwordtext.secureTextEntry = true;
//eyebutton
UIImage *eye = [UIImage imageNamed:#"eye.png"];
self.eyebutton = [UIButton buttonWithType:UIButtonTypeCustom];
self.eyebutton.frame = CGRectMake(290, 415, 20, 20);
[self.eyebutton setImage:eye forState:UIControlStateNormal];
[self.eyebutton addTarget:self action:#selector(touchDown) forControlEvents:UIControlEventTouchDown];
[self.eyebutton addTarget:self action:#selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
//FirstQuetionLabel
CGRect labelfq = CGRectMake(10, 450, 300, 20);
self.firstQuestlabel = [[UILabel alloc] initWithFrame:labelfq];
self.firstQuestlabel.font = [UIFont boldSystemFontOfSize:14.0f];
self.firstQuestlabel.text = #"Question 1: In which city you were born in?";
//FirstQuestionTextField
CGRect fqtextField = CGRectMake(10, 470, 240, 30);
self.firstQuestionText = [[UITextField alloc] initWithFrame:fqtextField];
self.firstQuestionText.borderStyle = UITextBorderStyleRoundedRect;
//SecondQuestionLabel
CGRect labelsq = CGRectMake(10, 500, 300, 30);
self.secondQuestLabel = [[UILabel alloc] initWithFrame:labelsq];
self.secondQuestLabel.font = [UIFont boldSystemFontOfSize:13.0f];
self.secondQuestLabel.text = #"Question 2: What is your mother's \rmiddle name?";
//SecondQuestionTextField
CGRect sqtextField = CGRectMake(10, 630, 240, 30);
self.secondQuestionText = [[UITextField alloc] initWithFrame:sqtextField];
self.secondQuestionText.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.eyebutton];
[self.view addSubview:self.firstnamelabel];
[self.view addSubview:self.firstnametext];
[self.view addSubview:self.lastnamelabel];
[self.view addSubview:self.lastnametext];
[self.view addSubview:self.DateOfBirthLabel];
[self.view addSubview:self.datepicker];
[self.view addSubview:self.emaillabel];
[self.view addSubview:self.emailtext];
[self.view addSubview:self.passwordlabel];
[self.view addSubview:self.passwordtext];
[self.view addSubview:self.firstQuestlabel];
[self.view addSubview:self.firstQuestionText];
[self.view addSubview:self.secondQuestLabel];
[self.view addSubview:self.secondQuestionText];
}
At first glance, scrolling may not be enabled because you have not set the contentSize property of the scroll view. To set allow scrolling, the contentSize needs to be set to be larger than the frame of the scroll view.
An example in objective-c would be:
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width + 20, self.myScrollView.frame.size.height + 20);
Updated for edited question
Any views you want to be scrollable within your scroll view need to be added as a subview on the scrollView i.e
[self.myScrollView addSubview: myLabel]; //do for all views that you want to be scrollable in the scrollView
Then you need find the sum height of all the views contained in your scroll view i.e
CGFloat sumHeight = myLabel1.frame.size.height + myView2.frame.size.height; //etc do for all views you want in your scrollView
then set it to be your contentSize ie
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width, sumHeight);

titleView jumps after segue

In my app I have a UINavigationController with a UISplitViewController as rootViewController. The masterViewController of this UISplitViewController sets a UIView with a UITextField centered inside it as titleView. This is wat it looks like in the storyboard.
- (void)viewDidLoad {
[super viewDidLoad];
self.titleView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-((self.view.frame.size.width/2)+150))/2, 0, (self.view.frame.size.width/2)+150, 30)];
self.rightButton = [[UIButton alloc] init];
[self.rightButton setImage:newImage forState:UIControlStateNormal];
[self.rightButton addTarget:self action:#selector(barButtonRight:) forControlEvents:UIControlEventTouchUpInside];
self.rightButton.frame = CGRectMake(0, 0, 30, 30);
self.rightButton.tintColor = [UIColor whiteColor];
[self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
self.splitViewController.navigationItem.rightBarButtonItem = rightButton;
self.textField = [[UITextField alloc]init];
if (IDIOM == IPAD) {
self.textField.frame = CGRectMake(0, 0, self.titleView.frame.size.width, 30);
} else {
self.textField.frame = CGRectMake(65, 0, (self.view.frame.size.width-20)-110, 30);
}
self.textField.backgroundColor = [UIColor purpleColor];
if (IDIOM == IPAD) {
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
self.textField.leftView = paddingView;
self.textField.leftViewMode = UITextFieldViewModeAlways;
}
[self.titleView addSubview:self.textField];
self.splitViewController.navigationItem.titleView = self.titleView;
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
Now, when a the right button is pressed a strange thing happens.
- (IBAction)barButtonRight:(id)sender {
[self performSegueWithIdentifier:#"showDetail" sender:self];
}
The UITextField is pushed all the way to the left inside the UIView. I commented out most of my viewDidLoad code and this is still happening. Any ideas as to what's causing this?

how to remove subview from class method in ios?

I have an app.In this app so many class methods used.I want to know is it possible to remove subview from class mehtod?
+(void)addPregressIndicator:(NSString *)strText view:(UIView *)ShowView
{
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView *busyView = [[UIView alloc] initWithFrame:appFrame];
busyView.backgroundColor = [UIColor clearColor];
busyView.opaque = YES;
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 95, 95)];
container.layer.borderWidth = 5;
container.layer.cornerRadius = 10;
container.layer.borderColor = [UIColor whiteColor].CGColor;
[container setBackgroundColor:[UIColor blackColor]];
UIActivityIndicatorView *av = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
av.center = CGPointMake(container.center.x, 34);
av.hidesWhenStopped = NO;
[av startAnimating];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 95, 30)];
lbl.text = strText;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.center = CGPointMake(container.center.x, 70);
lbl.textColor = [UIColor whiteColor];
lbl.backgroundColor = [UIColor clearColor];
[container addSubview:av];
[container addSubview:lbl];
container.center = busyView.center;
[busyView addSubview:container];
[ShowView addSubview:busyView];
}
+(void)removePregressIndicator:(UIView *)hideView;
{
// i want remove busyview that subview in viewcontroller.
}
Please help me to remove subivew.
You can set a unique tag for the busyView like this busyView.tag = your unique tag;
And then you can remove the view with this code
+(void)removePregressIndicator:(UIView *)hideView;
{
UIView *busyView = [hideView viewWithTag:your unique tag];
if (busyView){
[busyView removeFromSuperview];
}
}
You should make sure that the busyView tag is unique for all the subviews of hideView.
Assigne tag to a each view and access the view with tag and put it
+(void)removePregressIndicator:(UIView *)hideView;
{
UIView *viewToRemove = [self.view viewWithTag:assigne_tag];
[viewToRemove removeFromSuperview];
}
+ (void)removePregressIndicator:(UIView *)hideView;
{
UIView *progView=[self.view viewWithTag:YourTag];
[progView removeFromSuperview];
}

incompatible pointer types assigning to uilabel from label view

i've been doing a thing here using objective-c, but i'm a beginner in this language, and that's what i did:
I've created two UIView class and named them as LabelView and ButtonView. What i wanted to do is when i touch the button the label text changes. I have given the code below which i did.
I get an error when i touch the button i've created.
Error: "-[LabelView setText:]: unrecognized selector sent to instance 0x7b66cf80"
Warning on the line: self.cslbl_ = lblView; -
"incompatible pointer types assigning to uilabel from label view".
- (void)viewDidLoad {
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
//lblView.backgroundColor = [UIColor redColor];
self.cslbl_ = lblView;
[self.view addSubview:lblView];
ButtonView *btnView = [[ButtonView alloc] initWithFrame:CGRectMake(110, 200, 100, 50)];
SEL target = #selector(changeText:);
[btnView setSelector:self selector:target];
[self.view addSubview:btnView];
}
-(void) changeText:(UIButton *)btn {
static int k = 0;
if (k%2 == 0)
{
[self.cslbl_ setText:#"Apple is a healthy fruit"];
}
else
{
[self.cslbl_ setText:#"Apple"];
}
NSLog(#"Click value is %d",k);
k++;
}
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
Custom Button Class
-(id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIButton *myBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[myBtn setTitle:#"Touch" forState:UIControlStateNormal];
[myBtn setTitle:#"Touched" forState:UIControlStateHighlighted];
myBtn.backgroundColor = [UIColor grayColor];
self.btn_ = myBtn;
[self addSubview:myBtn];
}
return self;
}
- (void)setSelector:(UIViewController *)vc selector:(SEL)changeText
{
[btn_ addTarget:vc action:changeText forControlEvents:UIControlEventTouchUpInside];
}
Though question is not clear but it seems that your self.cslbl_ is a UILabel and you are assigning lblView to it which of type UIView, thats the reason you are getting warning.
And you are trying to setText to LabelView which is again not a UILabel type that why you are getting crash.
First of all you LabelView should be a subclass of UILabel not UIView but still if its your requirement then you should use below code.
Custom Label Class
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UILabel *myLbl = [[UILabel alloc] init];
myLbl.frame = CGRectMake(0, 0, 300, 50);
[myLb1 setTag:1];
//myLbl.backgroundColor = [UIColor blueColor];
myLbl.text = #"Apple";
self.lbl_ = myLbl;
[self addSubview:myLbl];
}
return self;
}
in viewdidload
LabelView *lblView = [[LabelView alloc] init];
[lblView setFrame:CGRectMake(10, 100, 300, 50)];
self.cslbl_ = (UILabel*)[lblView viewWithTag:1];
[self.view addSubview:lblView];
Now you can set text to your self.cslbl_

UIScrollView programmatically Scrolling

I have a UITextView inside a UIScrollView that needs to automatically scroll once the user starts editing it. This is because the keyboard will cover the textview.
Here is the code -
In viewDidLoad:
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segmentedControl.frame.origin.x + self.segmentedControl.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, emailField.frame.origin.y + 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
and here's the delegate method implementation -
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[scrollView setContentOffset:point animated:YES];
}
However, nothing happens.
your are doing fine but use feedBackformView instead of scrollView,while setting content offset in textViewDidBeginEditing:method, just have a look on below code
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
[feedBackformView addSubview:commentsView];
[self.view addSubview:feedBackformView];
in textview DelegateMethod,
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[feedBackformView setContentOffset:point animated:YES];
}
hope it will hepls you...
You can use scrollView scrollRectToVisible:animated or the setContentOffset:animated methods described here:
UIScrollView Class Reference
Keep in mind that UITextView comes with its own scroll view. so you may not need to nest it inside another scrollview. But you may need to if your app is that interesting.
Is your point of scrolling already at a visible point?
-(void)textViewDidBeginEditing:(UITextView *)textView{
// try this instead
CGPoint point = CGPointMake(textView.frame.origin.x,
textView.frame.origin.y + textView.frame.size.height);
[scrollView setContentOffset:point animated:YES];
// What does this produce?
NSLog(#"%# %#", NSStringFromCGPoint(scrollView.contentOffset),
NSStringFromCGRect(textView.frame));
}

Resources