How to assing a custom method inside a #selector XCode5 - macos

I have a NSAlert in XCode5
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Notificación"];
[alert setInformativeText:mensaje];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert beginSheetModalForWindow:window modalDelegate:nil
didEndSelector:#selector(myMethod})
contextInfo:nil];
as you see on didEndSelector I have a section for adding a method, is it possible to create there inside a custom method for making just an action, for instance something like this
[alert beginSheetModalForWindow:window modalDelegate:nil
didEndSelector:#selector(myCustomMethod{NSLog(#"hola");})
contextInfo:nil];
this is for saving the time of adding tons of methods one for each NSAlert
thanks in advance for the support

create separated method with some NSLOG for example
- (void)logs {
NSLog(#"lalalalalal")
}
Then send it to selector:
#selector(logs)
If your method receiving some variable, your select should be like:
#selector(logs:)

Related

How to display dialog box in OS X using Finder Sync Extension and Objective-C

I am creating a cocoa application with finder sync extension. I'm trying to open dialog box on some events of finder app.I am beginner for x code and objective c. I have tried the below code on some event..
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:#"OK"];
[alert addButtonWithTitle:#"Cancel"];
[alert addButtonWithTitle:#"OK"];
[alert addButtonWithTitle:#"Cancel"];
[alert setMessageText:#"Delete the record?"];
[alert setInformativeText:#"Deleted records cannot be restored."];
[alert setAlertStyle:NSWarningAlertStyle];
if ([alert runModal] == NSAlertFirstButtonReturn) {
// OK clicked, delete the record
}
But this code is just for presenting alert box.
Can anyone suggest me how do I get dialog box.
Try UIAlertView and UIAlertController or else simply use NSAppleScript

UIAlertView crash iOS7 - Assertion failure

I have a problem regarding the UIAlertView on iOS7.
When I launch my application, it crashes with the following message:
*** Assertion failure in -[UIKeyboardTaskQueue performTask:], /SourceCache/UIKit_Sim/UIKit-2903.2/Keyboard/UIKeyboardTaskQueue.m:388
The error occurs on the following line:
- (IBAction)updatePositions:(id)sender{
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show]; <====== IT CRASHS HERE
[NSThread detachNewThreadSelector:#selector(updateDataThread) toTarget:self withObject:nil];
}
I'm using ARC and the property _alert is set defined as: #property (nonatomic,strong)
This error seems strange, because on iOS6 the code works perfectly and I don't know what should be different on iOS7.
Does anyone have an idea what could the error?
Thanks in advance.
I encountered the same error, and the issue was that the UIAlertView was attempting to be shown from a thread which wasn't the main thread.
The crash however wouldn't always occur, only when a first AlertView was already being shown while this second AlertView was trying to pop up as well.
In my case, a simple fix was to do:
//Your code here
...
//Alert
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
dispatch_async(dispatch_get_main_queue(), ^{
//Show alert here
[_alert show];
});
//Resume your code here
...
I just had this problem after forgetting that I was working from a background thread. I don't know if that's the case here, but I'd make sure you're not trying to call updatePositions: from anything other than the main thread.
Change your code like this :
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show];
removing [[ and ]] around #"text"
But, your I don't think your problem came from this UIAlertView.
I had the same problem as well but not too familiar with the method dispatch_async. I used
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
and the problem hasn't come up again.
Put your alertview code in a separate function like
-(void)showAlert
{
_alert = [[UIAlertView alloc] initWithTitle:#"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show];
}
Then in your IBAction do this
- (IBAction)updatePositions:(id)sender
{
[self performSelectorOnMainThread:#selector(showAlert) withObject:nil waitUntilDone:YES];
[NSThread detachNewThreadSelector:#selector(updateDataThread) toTarget:self withObject:nil];
}
You can also do like this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Your title" message:#"Your message" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
However, if you need to show the same alert in multiple places it's better to create a separate function for it.

Pop-up message on macOS programming

Like using UIAlertView on iOS. There should be a similar solution for creating a pop-up message on macOS. Tried to search but could not find anything useful.
The class you're looking for is NSAlert. Information on this class can be found in its class reference here. And here is an example of its usage:
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Some awesome message text"];
[alert addButtonWithTitle:#"OK"];
[alert runModal];

Usernotes in ios

I want to create a user note form in my application,currently am using one textview inside a view its looking bad !! is there any other control suits for this purpose? Main aim is when user click the button a small textview will appear they can add comments there and save it into plist.
I want something like this(check the image)
i want that kind of usernotes (its my image) please give me some advices and helps to develop this..
Using UIAlertView with UITextView can be useful for you.
Implement UIAlertViewDelegate in .h file.
UITextView *comments;
-(IBAction)btnAddClicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Noreply Email" message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"Send", nil];
comments = [[UITextView alloc] initWithFrame:CGRectMake(15,45, 255, 100)];
[comments setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"mailbody.png"]]];
[comments setFont:[UIFont fontWithName:#"Helvetica" size:15]];
comments.scrollEnabled = YES;
[comments becomeFirstResponder];
[alert addSubview:comments];
[alert show];
[alert release];
}
Here alert will be prompted with small textview you can add comments and then handle text inside delegate method like this.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1) //Send button pressed
{
//handle your comment here
NSLog(#"%#",comments.text);
}
}

UIButton in UITableView header not working?

I have added a UIBUtton in a UITableView header and it doesn't seem to be working. It's clickable, but when attaching a IBAction to it, it fails and does nothing.
I have read online and saw to put it in your File's Owner, but I can't do that. When I put the action in, it only shows up in my First Responder.
I have also tried to put it in a different file, but when doing do it won't let me drag it to the other file.
FILES:
RootViewController.h
RootViewController.m
TableViewAppDelegate.h
TableViewAppDelegate.m
DetailViewController.h
DetailViewController.m
RootViewController.h
#interface RootViewController : UITableViewController {
NSMutableArray *listOfItems;
Sqlite *database;
NSTimer *myTimer;
UITextView *myTextField;
}
- (IBAction)addAlbum;
RootViewController.m
#import "RootViewController.h"
#import "TableViewAppDelegate.h"
#import "DetailViewController.h"
#implementation RootViewController
// If a user adds a photo album
- (IBAction)addAlbum {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Add Gallery" message:#"this gets covered" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Ok", nil];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, -80);
[alert setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[alert setTag:1];
}
Any help is appreciated!
Thanks.
If you're loading the table header view from .xib, make "File's Owner" of the .xib your ViewController subclass presenting the UITableView, and connect the desired event in the button to the desired IBAction in File's Owner. If you're creating the button programmatically in a tableView:viewForHeaderInSection: method, you must wire up the action yourself on the button using this method on the button object:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
EDIT: you should post your tableView:viewForHeaderInSection: code so we can give more specific advice.

Resources