0

How can I set the class for the same view when I preform a swipe on the view? I have already imported the header files for the classes that I want to change to for example #import "Circuit1.h"

- (IBAction)userSwiped:(UISwipeGestureRecognizer *)sender {
    [self numberOfSwips];

    self.numberOfSwips+=1;
    if (self.numberOfSwips>3){
        self.numberOfSwips=3;
    }
    if (self.numberOfSwips ==1) {
        self.myView.class =super.Circuit1;}
    else if (self.numberOfSwips ==2){
        self.myView.class =super.Circuit2;}
    else if (self.numberOfSwips ==3){
     self.myView.class =super.Circuit3;
    }
    NSLog(@"%f",self.numberOfSwips);
}

It's giving me an error for self.myView.class = super.Circuit1 and the other 2

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2988343
  • 205
  • 4
  • 8
  • property "Circuit1" is not found on object in "viewController – user2988343 Dec 01 '13 at 13:03
  • 1. Have you declared `circuit1` in the header? 2. Have you checked to see if the view controller exists in the `Compile Sources`? 3. Have you tried cleaning the project? (command+alt+shift+K) – Neeku Dec 01 '13 at 13:06
  • I did do 1 : #import "Circuit1.h" didnt get the second point , can u explain it more please – user2988343 Dec 01 '13 at 13:11
  • I asked if you've `declare`d it, not `import`ed it. Try 1, 2 and 3 and see if they help. – Neeku Dec 01 '13 at 13:13
  • how do u declare it ? – user2988343 Dec 01 '13 at 13:17
  • https://www.google.com/search?q=how+to+declare+an+ibar+in+objective+c&rlz=1C5CHFA_enIR503IR503&oq=how+to+declare+an+ibar+in+objective+c&aqs=chrome..69i57.6832j0j7&sourceid=chrome&espv=210&es_sm=91&ie=UTF-8 – Neeku Dec 01 '13 at 14:00
  • I dont think U can declare a class – user2988343 Dec 01 '13 at 14:39
  • Of course you cannot declare a `class`. Your error message says that `circuit1` is a `property` not a `class`: "property "Circuit1" is not found on object in "viewController" You need to read some documentations and tutorials. Study more. – Neeku Dec 01 '13 at 14:46
  • I understand what the error says . Im not sure wither or not self.myView.class =super.Circuit1 is the correct way to change the class of the view . if u know how to change the class of a UIView by using how please mention it , I looked in several books and web ites and I didnt find it – user2988343 Dec 01 '13 at 16:12

1 Answers1

0

Changing the class of an Objective-C object at runtime is technically possible, but is a bad idea. See Objective-C: How to change the class of an object at runtime? for a good explanation of why.

However, it is possible to declare a variable of type Class, and use the value stored in it to instantiate an object:

Class customClass = [Circuit1 class];

and then something like this:

[myView removeFromSuperview]; 
myView = [customClass alloc]initWithFrame:frame];
[containerView addSubView:myView]; 

but this would involve recreating the view each time the user swiped. I don't know what exactly your code does, but if the views are complex this is almost certainly the wrong way to go. If you only have 3 subviews, perhaps it would be better to create them as needed, store them in an instance variable array in the viewController, and swap them in and out of the view hierarchy when the user swipes

Community
  • 1
  • 1
Rich Tolley
  • 3,812
  • 1
  • 30
  • 39
  • thanks u , the reason why I want to be able to change the class of a view controller is to change the display where in there are different drawing in each class – user2988343 Dec 01 '13 at 20:16