Archive for 'Objective C'

Create UISegmentedControl programatically

You can create an UISegmented control in a cocos2d or Xcode project with the help of following lines code:

// Create an UIView

// Create the segmented control label array

// Create the UISegmentedControl and add to the UIView

Create UISwitch programatically

You can create an UIswitch in cocos2d or Xcode programatically with the help of following lines of code:

// Create an UIView

// Create the switch and add to the UIView

// Call function to get state

We can use the NSTemporaryDirectory() method to get the temporary directory path of an IOS application. The output from this method will be the entire path of the temporary directory of the application, if the directory is there. If the temporary directory is not present, then this will return nil. The output will be in string format.

You can use the following lines of code to check whether an image is in the app bundle or not in Xcode :

Incoming search terms:

  • xcode check to see if image exist

To formally conform to a protocol:

class_addProtocol([MyViewController class], @protocol(SomeProtocol));

Even more dynamically:

class_addProtocol(objc_getClass(“MyViewController”), objc_getProtocol(“SomeProtocol”));

To actually add method implementations to a class:

// – (int)someMethod:(int)arg;

int someMethod(id self, SEL _cmd, int arg)

{

return arg * 2;

}

 

class_addMethod([MyViewController class], @selector(someMethod:), (IMP)someMethod, “i@:i”);

How to change sprite’s speed dynamically?

You have to use a CCSpeed action:

CCSpeed* speed= [CCSpeed actionWithAction: yourMoveAction speed: 1.0f];

// yourMoveAction is an action like CCMoveTo for example

[sprite runAction: speed];

Then you can change the speed while the sprite moves with setSpeed:

[speed setSpeed: 2.0f];

Incoming search terms:

  • how to change a sprites speed
  • objective-c sprite speed

Custom action on Back Button UINavigationController

The solution is simple. You have to subclass your navigationController’s  popViewControllerAnimated:(BOOL)animated. So create a custom navigationController:

customNavigationController.h

#import

@interface customNavigationController : UINavigationController {}

@end

And a custom “popViewControllerAnimated:(BOOL)animated”, this popViewControllerAnimated-function uses the “UIViewAnimationTransitionCurlDown” when popping from a SettingsTableView.

customNavigationController.m

#import “customNavigationController.h”

#import “SettingsTableController.h”

 

@implementation customNavigationController

 

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

{

if([[self.viewControllers lastObject] class] == [SettingsTableController class]){

 

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration: 1.00];

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown

forView:self.view cache:NO];

 

UIViewController *viewController = [super popViewControllerAnimated:NO];

 

[UIView commitAnimations];

 

return viewController;

} else {

return [super popViewControllerAnimated:animated];

}

}

@end

Use your custom navigationController in your appDelegate:

customNavigationController *navigationController =

[[customNavigationController alloc]

initWithRootViewController:rootView];

 

 

 

Another Method

To Jules or anyone else who happens upon this page: I also needed a “are you sure you want to quit?” on the back button for my app. Here’s how I implemented it. Tricky but possible!

Turns out if you implement the UINavigationBarDelegate in the CustomNavigationController, you can make use of the shouldPopItem method:

—————————————————-

CustomNavigationController.h :

#import

@interface CustomNavigationController : UINavigationController {

BOOL alertViewClicked;
BOOL regularPop;
}

@end

—————————————————-
CustomNavigationController.m :

#import “CustomNavigationController.h”
#import “SettingsTableController.h”

@implementation CustomNavigationController

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

if (regularPop) {
regularPop = FALSE;
return YES;
}

if (alertViewClicked) {
alertViewClicked = FALSE;
return YES;
}

if ([self.topViewController isMemberOfClass:[SettingsTableViewController class]]) {
UIAlertView * exitAlert = [[[UIAlertView alloc] initWithTitle:@”Are you sure you want to quit?”
message:nil delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:@”Yes”, nil] autorelease];

[exitAlert show];

return NO;

}
else {
regularPop = TRUE;
[self popViewControllerAnimated:YES];
return NO;
}
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
//Cancel button
}

else if (buttonIndex == 1) {
//Yes button
alertViewClicked = TRUE;
[self popViewControllerAnimated:YES];
}

}
@end

Incoming search terms:

  • custom action back button ios
  • uinavigationcontroller custom back action
  • create own button uiviewcontroller
  • uiviewcontroller back button cancel
  • uinavigationitem back button clicked
  • uinavigationcontroller back button custom class
  • uinavigationcontroller back button action
  • uinavigationcontroller action back button
  • function to back uinavigationcontroller and refresh
  • custom navigationbar shouldpopitem

Find and replace particular character in a string

NSUInteger length = [insertDetails length];

NSRange range = NSMakeRange(0, length);

while(range.location != NSNotFound)

{

range = [insertDetails rangeOfString: @"*|" options:0 range:range];

if(range.location != NSNotFound)

{

if (([[NSString stringWithFormat:@"%c", [insertDetails characterAtIndex:range.location-1]] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@",()"]].location == NSNotFound) && ([[NSString stringWithFormat:@"%c", [insertDetails characterAtIndex:range.location+1]] rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@",()"]].location == NSNotFound))

{

insertDetails = [insertDetails stringByReplacingCharactersInRange:range withString:@"''"];

}

range = NSMakeRange(range.location + range.length, length – (range.location + range.length));

}

}

Incoming search terms:

  • how to find & replace particular string in another string in android

Setting corner radius and border of UIButton

//in .m//

// in viewDidLoad //

Incoming search terms:

  • ios set button corner radius
  • uibutton corner radius ios 6

Sending mail in Landscape mode : cocos2d

I used several codes to fix this problem. Finally, this just fixed my problem.

in .h file :

#import <MessageUI/MFMailComposeViewController.h>

@interface Game : CCLayer <MFMailComposeViewControllerDelegate>
{

NSString *emailTitle;
NSString *emailBody;
UIImage *emailImage;
MFMailComposeViewController *picker;
}
-(void)showMailPicker;
-(id)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image;

in .m file :

-(id) init
{
if( (self=[super init])) {

[self showMailPicker];

}
return self;
}

-(id)initWithTitle:(NSString *)title body:(NSString *)body image:(UIImage *)image
{
self = [super init];
if (self != nil) {
emailTitle = title;
emailBody = body;
emailImage = image;
[self showMailPicker];
}
return self;
}

-(void)showMailPicker
{
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.modalPresentationStyle = UIModalPresentationFullScreen;
picker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[picker setSubject:emailTitle];
[picker setMessageBody:emailBody isHTML:YES];

[[CCDirector sharedDirector] pause];
UIViewController *rootViewController = (UIViewController *)[[[CCDirector sharedDirector] openGLView ] nextResponder];  // This will do it

[rootViewController presentModalViewController:picker animated:YES];
[picker release];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[[CCDirector sharedDirector] resume];
[controller dismissModalViewControllerAnimated: YES];
}

 

Incoming search terms:

  • mail controller in cocos2d
  • mfmailcomposeviewcontroller landscape iphone
  • MFMailComposeViewController landscape iphone dev
  • send email form ios dev cocos2d
Page 1 of 1012345...10...Last »