ios - How to assign a new class in existing class? - iphone

I'm new to iPhone development,I want to assign one class in existing class
This is what declare in C#
public partial class UserRegisteration : System.Web.UI.Page
{
//some methods
public class Mime
{
//some methods
}
}
like the above format in Objective-C how to assign one more class in existing class?
Any ideas? Thanks in advance.

Yes you can.I am using one class in existing class . One is extending UIView and other is extending NSObject.
MultiLayout.h
#interface MultiLayout : UIView
{
// methods and properties
}
#end
#interface SingleControl : NSObject
{
// methods and properties
}
#end
MultiLayout.m
#implementation SingleControl
//methods and variables declaration and property synthesization
#end
#implementation MultiLayout
//methods and variables declaration and property synthesization
#end
For getting the static value of SingleControl in MultiLayout . you have to call class method like:
MultiLayout.h
#interface MultiLayout : UIView
{
// methods and properties
}
#end
#interface SingleControl : NSObject
{
// methods and properties
}
// add class method
+(int)getValue;
#end
MultiLayout.m
#implementation SingleControl
// add static value
static int values = 100;
// implement method
+(int)getValue{
return values;
}
#end
#implementation MultiLayout
// call method to get value
[SingleChoiceControl getValue];
#end

If I understand your question correctly, you would do this in your interface file:
#interface Mime
//properties and method declarations
#end
#interface UserRegistration : System.Web.UI.Page
#property (strong, nonatomic) Mime *mimeInstance;
#end
and then in the implementation file you implement both like so:
#implementation Mime
//implementation
#end
#implementation UserRegistration
//implementation
#end

Related

In Xcode how to suppress "incomplete implementation"?(inheritance)

I have a method which is implemented in its super method. And when I run my program. It works fine. How do I suppress this warning? By the way, both methods are in '.m' file.
The code is like this:
ClassA.m
#interface ClassA()
- (void)method;
#end
ClassA.h
#interface ClassA : ClassB
#end
ClassB.m
#interface ClassB()
#end
#implementation ClassB
- (void)method
{
}
And it works OK when I call method in ClassA
You could always implement the method anyway, and call the super method -
-(void)doMyThing {
[super doMyThing];
// do nothing
}
You've got it completly wrong. Popular construction is to have:
ClassB.h
#interface ClassB
- (void)method;
#end
ClassB.m
#interface ClassB()
#end
#implementation ClassB
- (void)method
{
//do nothing or even assert false
}
#end
ClassA.h
#interface ClassA : ClassB
#end
ClassA.m
#interface ClassA()
#end
#implementation ClassA
- (void)method
{
//do your stuff here
}
#end
And that's called method overriding. But if I correctly understand what you are trying to achieve you should have:
ClassB.h
#interface ClassB
- (void)method;
#end
ClassB.m
#interface ClassB()
#end
#implementation ClassB
- (void)method
{
//do your stuff here
}
#end
ClassA.h
#interface ClassA : ClassB
#end
And there is no need to mess up with ClassA, and you can call
ClassA *o=[[ClassA alloc ] init];
[o method];
Then the method is available in classA thanks to inheritance from ClassB.
Sounds like you've declared the method in the .h file when you shouldn't have? Or perhaps the #end is missing from the .h file.
You aren't giving us much to go on. Can we see your full code from the .h and .m?
Having read your code sample, try the following:
ClassA.h
#interface ClassA : ClassB
//ClassB inherits from Class A, so we define 'method' in Class B
//ClassA knows about 'method' by inheritance from class B,
//so 'method' doesn't need to be defined here.
//doStuff will be the method that calls 'method'
- (void)doStuff;
#end
ClassA.m
#interface ClassA
#end
#implementation ClassA
- (void)doStuff
{
//Call 'Method'
[self method];
}
#end
ClassB.h
#interface ClassB
//Define 'method' which will be inherited by ClassA
- (void)method;
#end
ClassB.m
#interface ClassB
#end
#implementation ClassB
- (void)method
{
//'method' does it's thing
NSLog(#"Method has been Called");
}
#end

how to pass string value from one class to another

I just want to know that how to pass the string value from one class to another..
Actually i have two classes.In first class i fetch string value from the array and i just want to use this string value into my second class.Now i don't know how to pass value between this two classes.Please give me some idea to do that.Should i use some class method to pass the value.But i don't know how to use this class methods.How i create the class methods to set the values from one class and then get the same value from class methods.
Thanks for help
Class1.h:
#interface Class1 : NSObject
{
NSArray *arr;
}
- (NSString *)myString;
#end
Class1.m:
#implementation Class1
- (NSString *)myString
{
return [arr objectAtIndex:0];
}
#end
Class2.h:
#interface Class2 : NSObject
{
}
- (void)methodThatUsesStringFromClass1:(Class1 *)c1;
#end
Class2.m:
#implementation Class2
- (void)methodThatUsesStringFromClass1:(Class1 *)c1
{
NSLog(#"The string from class 1 is %#", [c1 myString]);
}
#end
The simplest way is to define public #property in class where you want to pass your object, for example, for NSString:
// CustomClassA.h
#interface CustomClassA : NSObject
{
}
#property (nonatomic, retain) NSString *publicString;
#end
// CustomClassA.m
#implementation CustomClassA
#synthesize publicString;
#end
In your sender:
//somewhere defined CustomClassA objectA;
[objectA setPublicString:#"newValue"];
But you should understand what means retain, #synthesize and other. Also it is not your current question.
you can use appDelegate.YourStringVaraible =#"store your string";
and then use this YourStringVaraible in any class by using appDelegate.YourStringVaraible
pass the string parameter by overriding the init method.
Class1.m
#implementation Class1
Class2 *class2 = [[Class2 alloc] initWithString:myString];
...
#end
Class2.h
#interface Class2 : NSObject
{
NSString *string2;
}
#end
Class2.m
-(id) initWithString:(NSString*)str {
self = [super init];
if(self) {
string2 = str;
}
return(self);
}

Undeclared variable from base class when derived class has property

I have the following code below, where a base class has a member which is (should be) accessible by a derived class.
The code below however gives a compilation error
...abcAppDelegate.m:30: error: 'baseVal_' undeclared (first use in this function)
If I call the variable using self->baseVal_ or if I remove the property defined in the derived class then everything is ok.
Also, if I define a category of the derived class, then I can access baseVal_ without error.
//---------------------------------------------------------------
// BASE CLASS
//---------------------------------------------------------------
#interface BaseClass : NSObject
{
#protected
BOOL baseVal_;
}
#end
#implementation BaseClass
#end
//---------------------------------------------------------------
// DERIVED CLASS
//---------------------------------------------------------------
#interface DerivedClass : BaseClass {
}
#property (readwrite) BOOL val;
#end
#implementation DerivedClass
#synthesize val;
- (void) foo {
baseVal_ = YES;
}
#end
Have a look here: Click. Seems to possibly be a bug with GCC, but it's easily fixable by adding val as an instance variable instead of using the property without.

iPhone: Sharing protocol/delegate code

I have the following code protocol snippets:
#protocol FooDelegate;
#interface Foo : UIViewController {
id delegate;
}
...
#protocol FooDelegate
... // method 1
... // method 2
...
#end
Also, the following code which implements FooDelegate:
#interface Bar1 : UIViewController { ... }
#interface Bar2 : UITableViewController { ... }
It turns out the implementation of FooDelegate is the same on both Bar1 and Bar2 classes. I currently just copy FooDelegate implementation code from Bar1 to Bar2.
How do I structure/implement in such a way that Bar1 and Bar2 share the same code in a single code base (not as currently with 2 copies) since they are the same?
Thanks in advance for your help.
Option A: Implement the method in a Category
Any properties used must be declared in UIViewController.
UITableViewController is a subclass of UIViewController.
//UIViewController+MyAdditions.h
#interface UIViewController (MyAdditions)
- (void)myCommonMethod;
#end
//UIViewController+MyAdditions.m
#implementation UIViewController (MyAddtions)
- (void)myCommonMethod {
// insert code here
}
The new method added to UIViewController will be inherited by Bar1 and Bar2
Option B: Create a MyViewControllerHelper class
If you can, implement your common code as a class method, otherwise you will need to create an instance of your helper class either temporarily or as a property of Bar1 and Bar2
#interface MyViewControllerHelper : NSObject
- (void)myCommonMethod;
#end
#implementation MyViewControllerHelper
- (void)myCommonMethod {
// common code here
}
#interface Bar1 : UIViewController {
MyViewControllerHelper *helper;
}
#property MyViewControllerHelper *helper;
#end
#implementation Bar1
#synthesize helper;
- (void)someMethod {
[helper myCommonMethod];
}
#end
#interface Bar2 : UITableViewController {
MyViewControllerHelper *helper;
}
#property MyViewControllerHelper;
#end
#implementation Bar2
#synthesize helper;
- (void)someOtherMethod {
[helper myCommonMethod];
}
#end
Make a new object, MyFooDelegate:
#interface MyFooDelegate : NSObject <FooDelegate>
Then Bar1 and Bar2 can each create an instance of it (or share one instance). In those classes you can eliminate the delegate methods and add lines like:
MyFooDelegate *myDooDelegateInstance = ...;
foo.delegate = myFooDelegateInstance;
You could also create an instance of MyFooDelegate in a NIB file and connect the view controller's delegate outlets to it, if desired.
That way, you won't have any duplicated code in your source files or in your executables.

Can protocols within protocols be treated as inclusive of the protocol they adopt?

I am assigning protocols in a couple classes that follow an inheritance tree. Like so:
first class
#protocol LevelOne
- (void) functionA
#end
#interface BaseClass : NSObject <LevelOne> {
}
second class
#protocol LevelTwo <LevelOne>
- (void) functionB
#end
#interface SubClass : BaseClass <LevelTwo> {
}
Later I am assigning the class as delegate properties of other classes
base class
#interface AppClass : NSObject {
#protected
id<LevelOne> levelOneDelegate;
}
#property (assign) id<LevelOne> levelOneDelegate;
subclass
#interface AppClassesFriend : AppClass {
#protected
id<LevelTwo> levelTwoDelegate;
}
#property (assign) id<LevelTwo> levelTwoDelegate;
At the end of this journey, AppClassesFriend has 2 properties on it.
"levelOneDelegate" has access to "functionA", when it is assigned with a BaseClass object.
However, I am finding that "levelTwoDelegate" only has access to "functionB" (it is assigned with a SubClass object).
In order to have AppClassesFriend be able to use both functions, it seems I need to assign BOTH a levelOneDelegate AND levelTwoDelegate.
Is there any way to make "levelTwoDelegate" have access to both? Since, both functions are available on "SubClass".
So, what I would like to be able to do is :
SubClass *s = [SubClass alloc];
AppClassesFriend *a = [AppClassesFriend alloc];
a.levelTwoDelegate = s;
so inside AppClassesFriend (a) I could use :
[self.levelTwoDelegate functionA]; <---- this is never found
[self.levelTwoDelegate functionB];
but it seems I have to add
a.levelOneDelegate = s;
Thanks if anyone took the time to read all the way down this far. So in summary the question is, how do I get "levelTwoDelegate" to have access to both functionA and functionB?
Simply declare that your subclass's delegate property implements both level one and level two protocols (i.e. implements both functionA and functionB):
#interface AppClassesFriend : AppClass {
#protected
id<LevelOne,LevelTwo> levelOneAndTwoDelegate;
}
#property (assign) id<LevelOne,LevelTwo> levelOneAndTwoDelegate;