Dynamic cast to a custom class in objective C [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In NewTaskViewController.h, delegate was declared to be a property of type id.
Does the typecast below make delegate point to the object of type ViewController?
#import "NewTaskViewController.h"
#import "ViewController.h"
#implementation NewTaskViewController
- (IBAction)saveTask:(id)sender {
if ([self.textField.text length] == 0)
return;
ViewController *tasksListView = (ViewController *)self.delegate;
[tasksListView.tasks addObject:self.textField.text];
[self close:sender];
}
- (IBAction)close:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end

The cast tells the compiler that you want to use the delegate object as if it were a ViewController. If it really is one, you're OK. If it's not, bad things will happen at run-time. That is, the cast doesn't do any kind of conversion.

Related

iOS UIViewControler singleton and how to use helper class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I follow the online way to write singleton, each into SecondViewController, address of the print is the same , but why UISwitch interface is initialized ? I was into the original UIViewController? someone was also suggested to write helper class. Everthing is beginning, it is not very clear, Is there any Demo or explain the basic principles?How to write the code depend on second image?
enter image description here
enter image description here
Try this code
static SecondViewController *sharedVCInstance = nil;
+ (SecondViewController *)sharedInstance
{
static dispatch_once_t onceToken = 0;
dispatch_once (&onceToken, ^{
if(sharedVCInstance == nil)
sharedVCInstance = [[SecondViewController alloc] init];
});
return sharedVCInstance;
}

How to load interstitial ads when Navigation Back Button Pressed and Some Click Event? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to iOS development. I am adding an interstitial ads when ViewDidLoad but I want to show interstitial ads when an user clicks ten times in my application, is that possible? If it is possible then please help me finding a solution. My Application contains HMSegmentedControll and it has ten different UITableView. And I also want to display these ads when NavigationBar back button is pressed. Could anyone help me with this?
It is Possible like as SharedPreference in Android
Make One Global Variable NSObject like as
GlobalVariable.h File
#property (assign) int touchCount;
+ (TouchCount *)getInstance;
#end
and GlobalVariable.m File
#synthesize touchCount;
static TouchCount *instance = nil;
+(TouchCount *)getInstance
{
#synchronized(self)
{
if(instance==nil)
{
instance= [TouchCount new];
}
if (instance.touchCount ==10)
{
instance.touchCount=0;
instance= [TouchCount new];
}
}
return instance;
}
And Use this Instance when You Want to Touch Count import GLobalVariable.hlike as
TouchCount *obj=[TouchCount getInstance];
Whell u can add some global value than counts "back taps"
in GlobalVariables.h file something like this:
extern int PRJ_back_touches_count;
.m:
int PRJ_back_touches_count;
Than e.g. in viewWillDisappear method PRJ_back_touches_count++
On viewDidAppear u can handle the PRJ_back_touches_count actual value

Setting BOOLEAN in +(void) issue [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am trying to call BOOL in +(void) method but I can't. Why it would not set in +(void) method? While it is working on all -(void) methods.
.h
#property (nonatomic, assign) BOOL line;
.m
+ (void)normalizeCell:(UITableViewCell *)cell withLables:(NSArray *)lbls sx:(CGFloat)sx widths:(NSArray *)widths
if (![cell.contentView viewWithTag:22])
{
UIImageView *gb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"table.png"]];
gb = 22;
[cell.contentView addSubview:gb];
[gb release];
}
You can't use your header property in class method (+), only in instance method (-).
This is some kind of static method known from other programming language. In objective-c you can use class method to do some operation which fit to class you've created, but you have to remember that using a class method is NOT an object operation. You don't have to creating objects to use class method and of course you can't access properties of the objects.
+ methods are class level methods and properties are instance level variables. Therefor it's not possible to set them, on what instance would they be set? Class level methods should not be used if you need to keep state. You can keep state if you really want to like this.
+ (BOOL)myBool:(NSNumber *)boolValue{
static BOOL myBool = false;
if (boolValue){
myBool = [boolValue boolValue];
}
return myBool;
}
If you want this to not be part of the public interface just put this directly in the .m file so it's invisible for other classes. Then when you are inside your other class method you can do.
BOOL b = [self myBool:nil]; // get value
[self myBool:[NSNumber numberWithBool:YES]]; // set value
If you for reason would like to access this from your instances you can like this.
BOOL b = [MyClass myBool:nil];
[MyClass myBool:[NSNumber numberWithBool:NO]];

How to access UIView title property from another UIView? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to store the title of a view into an NSString but within a class method from one view to be used in another view but I just can't access the title. My code is this:
First view:
+(NSString*)title
{
NSString * strTitle = self.title;
return strTitle;
}
So, anyone could help me??
Thanks!!! . . .
You cant access an instance variable from a class method. So you have to change your method as an instance method
-(NSString*)title {
NSString *strTitle = self.title;
return strTitle;
}
Also make sure you set the title for this another viewcontroller before calling this method.
If you want to pass this data to another UIView class , then you have to make a title property for this UIView class. After initializing the object for this UIView, you can set this title property through this object.
You can set your view's title to viewTitle and get from it.
static NSString * viewTitle = nil ;
+ (NSString *)title {
return viewTitle ;
}
Code to use in secondviewcontroller:
#interface SecondViewController ()
#property(nonatomic,strong) NSString *titleString;
#end
#implementation ViewController
#synthesize titleString;
Code to use in firstviewcontroller:
SecondViewController * controllerObj =[[SecondViewController alloc]init];
controllerObj.titleString=NSStringFromClass([self class]);
[self.navigationController pushViewController:controllerObj animated:YES];
They are not possible for find name of .xib file name.

How to save the state of a iPhone Switch and print it out? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a switch on my ViewController, and whenever I press the save button, I want to use NSLog to print out if the switch is On/Off.
My question is, What data type saves the state of a switch? And how would I print out that variable with the state?
EDIT:
If it was a DatePicker, it would be
#property (nonatomic, strong) NSDate *pickerDate;
I need the Switch version of this. NSSwitch doesn't seem to work.
UISwitch has a BOOL property called "on". Say your switch is called "theSwitch"
if(theSwitch.on) {
NSLog(#"on")
}
else {
NSLog(#"off")
}
#property (nonatomic, strong) UISwitch *aSwitch;
if(aSwitch == nil)
NSLog("Oops, switch is nil, you cannot save its state");
NSLog(#"switch is:%#", aSwitch.on? #"on", #"off");

Resources