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

    UIView *profileView = [[UIView alloc] init];
    profileView.frame = CGRectMake(0, 50, 320, 395);
    profileView.backgroundColor = [UIColor clearColor];
    [[[CCDirector sharedDirector] openGLView] addSubview:profileView];

// Create the segmented control label array

    NSArray *itemArray = [NSArray arrayWithObjects: @"Credit", @"Debit", nil];

// Create the UISegmentedControl and add to the UIView

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] 
                                                      initWithItems:itemArray];
    segmentedControl.frame = CGRectMake(5, 20, 150, 35);
    segmentedControl.center = CGPointMake(220, (50*5));
    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
    segmentedControl.selectedSegmentIndex = 1;
    segmentedControl.tintColor = [UIColor 
                         colorWithRed:0.1f green:0.4f  blue:0.65f  alpha:1.0f];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

    [segmentedControl addTarget:self
    action:@selector(segmentChanged:)
    forControlEvents:UIControlEventValueChanged];
   [profileView addSubview:segmentedControl];

Create UISwitch programatically

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

// Create an UIView

    UIView *profileView = [[UIView alloc] init];
    profileView.frame = CGRectMake(0, 50, 320, 395);
    profileView.backgroundColor = [UIColor clearColor];
    [[[CCDirector sharedDirector] openGLView] addSubview:profileView];

// Create the switch and add to the UIView

    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:
                                           CGRectMake(130, 235, 0, 0)];
    [mySwitch addTarget:self action:@selector(changeSwitch:) 
                   forControlEvents:UIControlEventValueChanged];
    [profileView addSubview:mySwitch];

// Call function to get state

    -(void)changeSwitch:(UISwitch *)mySwitch{
        NSLog(@"%@", mySwitch.on ? @"On" : @"Off");
    }

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.

Incoming search terms:

  • C find tmp on iOS
  • ios get temp directory

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

-(void)CheckForImageInBundle
{
NSString *exists = [[NSBundle mainBundle]pathForResource:@"Image" ofType:@"png"];  //This stores the path of the resource with the name "Image" and of type "png" to the string. Nothing will be stored if there is no such file.
if(exists == nil){
NSLog(@"Doesn't exist");
}
else{
NSLog(@"Exists!");
}
}

Incoming search terms:

  • android check if items exists in bundle
  • ios 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];

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:

  • uinavigation controller back button custom action
  • uinavigationcontroller back event custom

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:

  • replacing partcular character in NSString ios

Setting corner radius and border of UIButton

//in .m//

    #import <QuartzCore/QuartzCore.h>

// in viewDidLoad //

     /*  -- To set corner radius--  */
         btn1_Calender.layer.cornerRadius = 15;
         btn1_Calender.clipsToBounds = YES;
    /*  -- To set button border with color--  */
         [[btn1_Calender layer] setBorderWidth:1.8f];
         [[btn1_Calender layer] setBorderColor:[UIColor whiteColor].CGColor];

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];
}

 

Page 1 of 1012345...10...Last »
We Can Build Your App for You. Lets Get Started !