iphone: unrecognized selector sent to instance - iphone

I am new to objective C, So maybe there is some basic thing that I am missing about selectors. I would like to understand the basic concept behind this error as I have not found an general error reference.
I am getting this error when using:
[CloseButton addTarget:PageContents action:#selector(CloseButtonPressed) forControlEvents:UIControlEventTouchUpInside];
and then later:
- (void)CloseButtonPressed:(id)sender{
UIAlertView *someError = [[UIAlertView alloc] initWithTitle: #"Comment" message: #"hello" delegate: self cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[someError show];
[someError release];
}

A couple suggestions that will help your code follow writing conventions used by all Objective C applications, and make your code more easily readable to others:
Object instances should be lower case, i.e. closeButton and not CloseButton, and pageContents, not PageContents
Method names should be lower case, i.e. -closeButtonPressed: and not -CloseButtonPressed:
To answer your question, you need to fix the action you are adding:
[CloseButton addTarget:PageContents action:#selector(CloseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
That colon character (:) makes sure the sender gets through to -CloseButtonPressed:

As CloseButtonPressed takes parameter you should create selector using: #selector(CloseButtonPressed:)

Related

UIAlertView maximum text length

I just want to show some text on the UIAlertView but it is showing "Null" string if the text length larger than allowed size.
I am pretty sure the text will not larger than the screen (about a bit more than half), so I don't want to make it be complicated to implement a ScrollView for that.
I follow problem in changing size of uialertview to change the size of AlertView, but it does not work, additionally produce some weird visual effect.
I tried this third part component https://github.com/inamiy/YIPopupTextView, I can't even pass the compilation. (already import that 4 files into project.) I don't know why.
So, actually I just want to increase the size of text that allowed to show on AlertView. Any idea?
I wrote a tiny test program. Here's the only thing I added to the Single View Application template:
- (void)viewDidAppear:(BOOL)animated {
NSMutableString *message = [[NSMutableString alloc] init];
while (message.length < 100000) {
[message appendString:#"Hello, world! "];
}
[message appendString:#"This is the end."];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test" message:message delegate:nil cancelButtonTitle:#"Done" otherButtonTitles:nil];
[alert show];
}
This works fine on the iPhone simulator running iOS 5.0, and on my iPhone 5 running iOS 6.0.2. All the text is displayed (in a scrollable text view).
Your problem is probably not with the size of the text.
I found a strange behavior while I was showing the result of a JSON object parsed in a dictionary and then printed on on an alertView (on Xcode 5.1.1, compiling for iOS 7.1 on iphone 64 bit simulator). For the same inputData:
[[UIAlertView alloc]initWithTitle:#"something" message:[[[NSString stringWithFormat:#"json:%#",[inputData dictionaryRepresentation]] stringByReplacingOccurrencesOfString:#" "withString:#""]substringToIndex:7035] delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil];
correctly prints, but if I say "substringToIndex:7036" it shows only blank space...without "stringByReplacingOccurrencesOfString:" method the limit is far beyond:
[[UIAlertView alloc]initWithTitle:#"something" message:[[NSString stringWithFormat:#"json:%#",[inputData dictionaryRepresentation]] substringToIndex:13768] delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil];
correctly prints, instead "substringToIndex:13769" not prints...I realize that is not a matter of maximum length but of special characters inside the JSON object

What is wrong with this Tweak for Cydia?

I wrote a tweak for cydia, but it does not seem to be working. I created the template using Theos. I got a header dump from the iosod tool, and found
`$` - (void)searchBarTextDidBeginEditing:(id)searchBarText;
inside of the SBSearchController class. Here is the code I have in the Tweak.
%hook SBSearchController
- (void)searchBarTextDidBeginEditing:(id)searchBarText
{
%orig;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Tweak"
message:#"Testing is running!"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];`
}
%end
and my Makefile looks like this
include theos/makefiles/common.mk
Testing_FRAMEWORKS = UIKit
TWEAK_NAME = Testing
Testing_FILES = Tweak.xm
include $(THEOS_MAKE_PATH)/tweak.mk \
Everything compiles and installs correctly, but at runtime, when I tap on the search bar, and begin to type, nothing happens. Does anybody know what I did wrong?
Thanks!
Remember that since this is a delegate method it won't be called unless the delegate class implements the method. Have you tested this on an application which handles this event?
Try adding some logging to a file so you can see whether your code is being called or not.

AlertView with textfield - beginner

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"HELOOOOOOOO" message:#"write a number of text-lines/paragraph here"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 95, 260, 25)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
If you look close you will find that the message attribute is quite long. I need this alertview to first display the title, and then my long message, and then the text field and finally the 2 buttons.
But what hapence here, is that since the message is too long, the textfield overlaps with the buttons.
How can i solve this ?
If this is for iPhone, you really can't. The UIAlertView is not meant to handle input. Extended messages are shown with a scroll view but you really should not add a UITextField to it's view hierarchy (for one, it goes against their design standards and your app MAY get nixed!).
In this situation, it would probably be best to use a new UIViewController to handle showing your content.
Again, the only "actions" that UIAlertView is meant to provide is that of the multiple buttons.
Implement this piece of code in your implementation file.
- (void)willPresentAlertView:(UIAlertView *)alertView
{
alertview.frame = CGRectMake(12, 95, 260, 25); //You can set a frame that suits to your needs
}
This delegate - (void)willPresentAlertView:(UIAlertView *)alertView will invoke before showing the alerview, so it is a better way to set frame to your alertview.
For more informations on aleert views:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/occ/intf/UIAlertViewDelegate
Actually, it doesn't matter how long your message is. The alert view will always size so it fits.
One way you can fix this is by adding a bunch of \n at the end of your message string, which is equivalent to putting a line break.
EDIT: If your message is so long that it puts it in a UIScrollView, there's nothing you can really do unless you're fine with major hacking (aka, changing the bounds of the UIAlertView, moving each button down, etc.).
A second way works only on iOS 5 and newer. You can set the UIAlertView's alertStyle property to UIAlertViewStylePlainTextInput which will display an alert view with a text field.
Use newline or line break character \n for this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"AlertView"message:#"\nhello:.................... \nwrite here.................. \nwrite here....................." delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Yes", nil];
[alert show];
[alert release];
Check this blog post for more help

UIAlertView with UITextField and long message

I am using this code to implement a UITextField in my UIAlertView:
UIAlertView *receivedAlert = [[UIAlertView alloc] initWithTitle:message message:[NSString stringWithFormat:#"%#\n\n", [itemNameRows objectAtIndex:currentItem]] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
receivedField = [[UITextField alloc] initWithFrame:CGRectMake(16,70,252,25)];
receivedField.keyboardType = UIKeyboardTypeNumberPad;
receivedField.textAlignment = UITextAlignmentCenter;
receivedField.borderStyle = UITextBorderStyleRoundedRect;
receivedField.keyboardAppearance = UIKeyboardAppearanceAlert;
[receivedAlert addSubview:receivedField];
[receivedAlert show];
[receivedAlert release];
The problem arises when I have a message that is longer than 2 lines. The alert view resizes properly but the text field doesn't move down to accommodate the longer line. Any ideas?
I can settle with shortening the message string if I have to.
You shouldn't add subviews to UIAlertView. Apple's docs explicitly prohibit it ("The view hierarchy for this class is private and must not be modified."), and in addition to possibly getting you rejected, could also break in a future OS update (this happened once already around the iOS 3.1).
iOS 5 added alert view styles so that you can use text fields in alert views in a safe way. In addition to UIAlertViewStyleDefault, there are UIAlertViewStyleSecureTextInput, UIAlertViewStylePlainTextInput, and UIAlertViewStyleLoginAndPasswordInput.
Create an alert view and set the alertViewStyle property to the appropriate value. You can then get the text field(s) by using -textFieldAtIndex:. Secure and plain text styles have a single text field at index 0, default has no text fields, and login and password have two text fields at indexes 0 (login) and 1 (password).
See the UIAlertView Class Reference for more information.

How to create the complex UIAlert View in iphone

I am trying to create the complex UIAlert View .But i am not getting right mark to get it work
Actaully i need the UIAlertView is like this
It has the png image on the leftside upper corner .And in same line it has the UIAlert titleBar.
Below that it has the message of two line
And it's below it has the TextFiled .and below the textfield it has
the once again same TextMessage
And last but not in list it has the Two button .
And ever user enter the text value in textfield and press the OK BUtton it get show on the label also .
I am trying coding like this But it not getting work what was the problem in it
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Set Tags"
message:#"Do you want to add tag.\nExample:-My Content"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message addTextFieldWithValue:#"" label:#"Enter tag Name"];
[message addButtonWithTitle:#"Cancel"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 30, 30)];
NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"tag.png"]];
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:bkgImg];
[bkgImg release];
[path release];
[message addSubview:imageView];
if(FALSE)
{
[message addButtonWithTitle:#"Button 4"];
}
[message show];
[message release]
;
Help me out from this
Thank you.
Harish,
I am having a hard time understanding your english so I may not be hitting the mark with this answer. However, If I am understanding the question correctly then I believe I see only one major issue with your code.
From the documentation that I have read on UIAlertView I do not see the method "addTextFieldWithValue" as part of that class. There is however a reference to UIAlertViewStyle which is a property that can be set that will allow you to tell the class to display itself with a text field. You can find the class reference here
[message addTextFieldWithValue:#"" label:#"Enter tag Name"];
//This will not work.
//Instead use this.
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
Of course you will need to make sure you have the proper delegate methods added to retrieve the value once buttons are clicked.
I've taken your code and created an example project with only a few minor changes. Using my above suggestion the results I get are reflected in the screen shot I have taken below.
UIAlertView is not the component you should be looking to use in this case. It is not supposed to be customized. You should be displaying a modal view (which consists of all the items you listed) and display it using presentModelViewController:animated:.